乐趣区

第六章第三十三题(当前日期和时间)(Current date and time) – 编程练习题答案

**6.33(当前日期和时间)调用 System.currentTimeMillis() 返回从 1970 年 1 月 1 日 0 点开始至今为止的毫秒数。编写程序,显示当前日期和时间。
下面是运行示例:

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 语言程序设计与数据结构(基础篇)(原书第 11 版)Java 语言程序设计(基础篇)(原书第 10/11 版)更多

退出移动版