Capítulo VI Pregunta 33 (Fecha y hora actual)
** 6.33 (Fecha y hora actuales) Llame a System.currentTimeMillis () para devolver el número de milisegundos desde las 0:00 del 1 de enero de 1970. Escriba un programa para mostrar la fecha y hora actuales. A continuación se muestra un ejemplo de ejecución: La fecha y hora actuales es el 16 de mayo de 2012 10:34:23 ** 6.33 (Fecha y hora actuales) Invocar System.currentTimeMillis () devuelve el tiempo transcurrido en milisegundos desde la medianoche del 1 de enero de 1970. Escriba un programa que muestra la fecha y la hora. A continuación, se muestra una ejecución de muestra: La fecha y hora actuales es el 16 de mayo de 2012 10:34:23
Código de referencia:
package chapter06;
public class Code_33 {
public static void main ( String[ ] args) {
long totalMilliseconds = System. currentTimeMillis ( ) ;
int totalDays = ( int ) ( totalMilliseconds / 1000 / 60 / 60 / 24 ) ;
long totalSeconds = totalMilliseconds / 1000 ;
long currentSecond = totalSeconds % 60 ;
long totalMinutes = totalSeconds / 60 ;
long currentMinute = totalMinutes % 60 ;
long totalHours = totalMinutes / 60 ;
long currentHour = totalHours % 24 ;
int currentYears = 1970 , currentMonths = 1 , currentDays;
while ( totalDays >= 365 ) {
if ( isLeapYear ( currentYears) )
totalDays -= 366 ;
else
totalDays -= 365 ;
currentYears++ ;
}
while ( totalDays >= 28 ) {
if ( currentMonths == 1 || currentMonths == 3 || currentMonths == 5 || currentMonths == 7
|| currentMonths == 8 || currentMonths == 10 || currentMonths == 12 ) {
totalDays -= 31 ;
currentMonths++ ;
} else if ( currentMonths == 4 || currentMonths == 6 || currentMonths == 9 || currentMonths == 11 ) {
totalDays -= 30 ;
currentMonths++ ;
}
else if ( isLeapYear ( currentYears) && currentMonths == 2 ) {
totalDays -= 29 ;
currentMonths++ ;
} else {
totalDays -= 28 ;
currentMonths++ ;
}
}
if ( totalDays == 0 )
currentDays = 1 ;
else
currentDays = totalDays + 1 ;
System. out. printf ( "Current date and time is %s %d, %d %d:%d:%d" ,
MonthEnglish ( currentMonths) , currentDays, currentYears,
currentHour, currentMinute, currentSecond) ;
}
public static boolean isLeapYear ( int year) {
return ( year % 400 == 0 || ( year % 100 != 0 && year % 4 == 0 ) ) ;
}
public static String MonthEnglish ( int month) {
String monthString;
switch ( month)
{
case 1 :
monthString = "January" ;
break ;
case 2 :
monthString = "February" ;
break ;
case 3 :
monthString = "March" ;
break ;
case 4 :
monthString = "April" ;
break ;
case 5 :
monthString = "May" ;
break ;
case 6 :
monthString = "June" ;
break ;
case 7 :
monthString = "July" ;
break ;
case 8 :
monthString = "August" ;
break ;
case 9 :
monthString = "September" ;
break ;
case 10 :
monthString = "October" ;
break ;
case 11 :
monthString = "November" ;
break ;
case 12 :
monthString = "December" ;
break ;
default :
monthString = "" ;
}
return monthString;
}
}
Los resultados muestran que:
Current date and time is October 21 , 2020 15 : 10 : 33
Process finished with exit code 0