第六章第三十三题(当前日期和时间)(Current da…

2020-05-22 16:08:03来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

第六章第三十三题(当前日期和时间)(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版)更多


原文链接:https://www.cnblogs.com/in2013/p/12937758.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: LeetCode 210. 课程表 II

下一篇:java 类加载系统