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版)更多内容

  • 相关阅读:
    C#调用自定义表类型参数
    不同版本SQL SERVER备份还原时造成索引被禁用
    SQL SERVER同步环境新增发布对象时不能生成(sp_MS+表名)同步存储过程
    C# 读取在存储过程多结果集
    C#读取XML文件
    批量还原V2
    tmux 常用快捷键
    无法生成SSPI上下文
    sql server 性能计数器
    sql server 2008 r2 xevent
  • 原文地址:https://www.cnblogs.com/in2013/p/12888481.html
Copyright © 2011-2022 走看看