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

2022-03-29 13:12:51 浏览数 (1)

**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

下面是参考答案代码:

代码语言:javascript复制
// 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);
    }
}

0 人点赞