zoukankan      html  css  js  c++  java
  • 第六章第二十四题(显示当前日期和时间)(Display current date and time)

    **6.24(显示当前日期和时间)程序清单2-7显示当前时间。改进这个例子,显示当前的日期和时间。程序清单6-12中日历例子可以提供一些如何求年、月和日的思路。

    **6.24(Display current date and time) Listing 2.7, ShowCurrentTime.java, displays the current time. Revise this example to display the current date and time. The calendar example in Listing 6.12, PrintCalendar.java, should give you some ideas on how to find the year, month, and day.

    下面是参考答案代码:

    // https://cn.fankuiba.com
    public class Ans6_24_page202 {
        /** Main method */
        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;
            }
    
            // Display results
            System.out.println("Current data is " + currentYear +
                    "-"+currentMonths+"-"+currentDay+ "
    Current time is " +
                    currentHour+":"+currentMinute+":"+currentSecond+" (GMT+8)");
        }
    
        /** 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版)更多内容

  • 相关阅读:
    html 中 url、scr、href、rel、rev
    MIME 和文件扩展名
    视频文件的容器格式和编码格式
    原型与原型链
    属性特征
    可选参数
    函数的定义(函数是值)
    闭包
    实现异步加载js文件及加载完成后回调
    前端工程打开速度优化的循序渐进总结
  • 原文地址:https://www.cnblogs.com/in2013/p/12888481.html
Copyright © 2011-2022 走看看