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

  • 相关阅读:
    luogu 5311 [Ynoi2011]D1T3 动态点分治+树状数组
    LOJ #6145. 「2017 山东三轮集训 Day7」Easy 点分树+线段树
    BZOJ 2117: [2010国家集训队]Crash的旅游计划 动态点分治+二分
    BZOJ 1095: [ZJOI2007]Hide 捉迷藏 动态点分治+堆
    BZOJ 3924: [Zjoi2015]幻想乡战略游戏 动态点分治
    luogu 3241 [HNOI2015]开店 动态点分治+二分+vector
    luogu 2993 [FJOI2014]最短路径树问题 Dijkstra+点分治
    BZOJ 3697: 采药人的路径 点分治
    启动elasticsearch报错的几种原因及解决方法
    SpringBoot与MybatisPlus3.X整合示例(十六)
  • 原文地址:https://www.cnblogs.com/in2013/p/12888481.html
Copyright © 2011-2022 走看看