第六章第三十三题(当前日期和时间)(Current date and time)

第六章第三十三题(当前日期和时间)(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
  • 参考代码:
package chapter06;

public class Code_33 {
    
    
    public static void main(String[] args) {
    
    
        long totalMilliseconds = System.currentTimeMillis();
        int totalDays = (int) (totalMilliseconds / 1000 / 60 / 60 / 24);
        long totalSeconds = totalMilliseconds / 1000;
        long currentSecond = totalSeconds % 60;
        long totalMinutes = totalSeconds / 60;
        long currentMinute = totalMinutes % 60;
        long totalHours = totalMinutes / 60;
        long currentHour = totalHours % 24;
        int currentYears = 1970, currentMonths = 1, currentDays;
        while (totalDays >= 365) {
    
    
            if (isLeapYear(currentYears))
                totalDays -= 366;
            else
                totalDays -= 365;
            currentYears++;
        }
        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(currentYears) && currentMonths == 2) {
    
    
                totalDays -= 29;
                currentMonths++;
            } else {
    
    
                totalDays -= 28;
                currentMonths++;
            }
        }
        if (totalDays == 0)
            currentDays = 1;
        else
            currentDays = totalDays + 1;
        // Display results
        System.out.printf("Current date and time is %s %d, %d %d:%d:%d",
                MonthEnglish(currentMonths), currentDays, currentYears,
                currentHour, currentMinute, currentSecond);
    }
    public static boolean isLeapYear(int year) {
    
    
        return (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0));
    }
    public static String MonthEnglish(int month) {
    
    
        String monthString;
        switch(month)
        {
    
    
            case 1:
                monthString =  "January";
                break;
            case 2:
                monthString = "February";
                break;
            case 3:
                monthString = "March";
                break;
            case 4:
                monthString = "April";
                break;
            case 5:
                monthString = "May";
                break;
            case 6:
                monthString = "June";
                break;
            case 7:
                monthString = "July";
                break;
            case 8:
                monthString = "August";
                break;
            case 9:
                monthString = "September";
                break;
            case 10:
                monthString = "October";
                break;
            case 11:
                monthString = "November";
                break;
            case 12:
                monthString = "December";
                break;
            default:
                monthString = "";
        }
        return monthString;
    }
}

  • 结果显示:
Current date and time is October 21, 2020 15:10:33
Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/jxh1025_/article/details/109211958
今日推荐