**6.33(當(dāng)前日期和時(shí)間)調(diào)用System.currentTimeMillis()返回從1970年1月1日0點(diǎn)開始至今為止的毫秒數(shù)。編寫程序滨攻,顯示當(dāng)前日期和時(shí)間。
下面是運(yùn)行示例:
Current date and time is May 16, 2012 10:34:23
**6.33(Current date and time) Invoking System.currentTimeMillis() returns the elapsed time in milliseconds since midnight of January 1, 1970. Write a program that displays the date and time.
Here is a sample run:
Current date and time is May 16, 2012 10:34:23
下面是參考答案代碼:
// https://cn.fankuiba.com
public class Ans6_33_page205 {
public static void main(String[] args) {
// Obtain the total milliseconds since midnight, Jan 1, 1970
long totalMilliseconds = System.currentTimeMillis();
// Obtain the total seconds since midnight, Jan 1, 1970
long totalSeconds = totalMilliseconds / 1000;
// Compute the current second in the minute in the hour
long currentSecond = totalSeconds % 60;
// Obtain the total minutes
long totalMinutes = totalSeconds / 60;
// Compute the current minute in the hour
long currentMinute = totalMinutes % 60;
// Obtain the total hours
long totalHours = totalMinutes / 60;
// Compute the current hour
long currentHour = totalHours % 24;
long totalDays = totalHours / 24;
int currentYear = 1970;
while (totalDays >= 365) {
if (isLeapYear(currentYear))
totalDays -= 366;
else
totalDays -= 365;
currentYear++;
}
int currentMonths = 1;
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(currentYear) && currentMonths == 2) {
totalDays -= 29;
currentMonths++;
} else {
totalDays -= 28;
currentMonths++;
}
}
long currentDay;
if (totalDays == 0)
currentDay = 1;
else
currentDay = totalDays + 1;
// GMT+8
if (currentHour+8 >= 24) {
currentHour = currentHour+8-24;
}
// IntMonthToStrMonth
String strCurrentMonths = "";
switch (currentMonths) {
case 1:
strCurrentMonths = "January";break;
case 2:
strCurrentMonths = "February";break;
case 3:
strCurrentMonths = "March";break;
case 4:
strCurrentMonths = "April";break;
case 5:
strCurrentMonths = "May";break;
case 6:
strCurrentMonths = "June";break;
case 7:
strCurrentMonths = "July";break;
case 8:
strCurrentMonths = "August";break;
case 9:
strCurrentMonths = "September";break;
case 10:
strCurrentMonths = "October";break;
case 11:
strCurrentMonths = "November";break;
case 12:
strCurrentMonths = "December";
}
// Display results
System.out.println("Current date and time is " + strCurrentMonths
+" "+currentDay+", "+currentYear+" "+
currentHour+":"+currentMinute+":"+currentSecond);
}
/** Determine if it is a leap year */
public static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
}
適用Java語言程序設(shè)計(jì)與數(shù)據(jù)結(jié)構(gòu)(基礎(chǔ)篇)(原書第11版)Java語言程序設(shè)計(jì)(基礎(chǔ)篇)(原書第10/11版)更多