第VI章質問33(現在の日時)

第VI章質問33(現在の日時)

  • ** 6.33(現在の日付と時刻)System.currentTimeMillis()を呼び出して、1970年1月1日の0:00からのミリ秒数を返します。現在の日付と時刻を表示するプログラムを作成します。
    実行例を次に示します。
    現在の日付と時刻は2012年5月16日10:34:23
    ** 6.33(現在の日付と時刻)System.currentTimeMillis()を呼び出すと、1970年1月1日の深夜からの経過時間がミリ秒単位で返されます。日付と時刻を表示するプログラム。
    実行例は次のとおりです。
    現在の日付と時刻は2012年5月16日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