zoukankan      html  css  js  c++  java
  • 第六章第三十三题(当前日期和时间)(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版)更多

  • 相关阅读:
    Jvm年轻代复制到Survivor To区时,对象存放不下会发生什么?
    Jvm内存布局和Java对象内存布局
    ArrayList的removeIf和iterator.remove性能比较
    闲着没事做,用js做了一个冒泡排序的动画
    对象与this
    idea 简记
    线程按序交替
    大数阶乘
    序列化 与 反序列化
    人月神话
  • 原文地址:https://www.cnblogs.com/in2013/p/12937758.html
Copyright © 2011-2022 走看看