Introduction to Java Programming编程题5.33<显示当前日期和时间>

/*
Current data and time is August 18, 2015 6:8:23.
*/
public class CurrentTime {
    public static void main(String[] args) {
        showTime();
    }

    public static void showTime() {
        final long PER_SECONDS_DAY =  60 * 60 * 24;
        long totalSeconds = (System.currentTimeMillis() + 8 * 60 * 60 * 1000) / 1000;
        long totaldays = totalSeconds / PER_SECONDS_DAY;

        System.out.print("Current data and time is ");
        showCurrentData(totaldays);
        showCurrentTime(totalSeconds);
    }

    public static void showCurrentTime(long totalSeconds) {
        long seconds = totalSeconds % 60;
        long minutes = totalSeconds / 60 % 60;
        long hours = totalSeconds / 60 / 60 % 24;
        System.out.println(" " + hours + ":" + minutes + ":" + seconds + ".");
    }

    public static boolean isLeapYear(long year) {
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
            return true;
        else
            return false;
    }

    public static void showCurrentData(long totaldays) {
        final int START_YEAR = 1970;
        int days, currentYear;

        for (currentYear = START_YEAR; totaldays >= 365; currentYear++) {
            if (isLeapYear(currentYear))
                days = 366;
            else
                days = 365;
            totaldays -= days;
        }

        int month = 1;
        int n = perMonthDay(currentYear, month);
        while (totaldays > n) {
            totaldays -= n;
            month++;
            n = perMonthDay(currentYear, month);
        }
        totaldays++;
        System.out.print(toMonthLetter(month) + " " + totaldays + ", " + currentYear);
    }

    public static String toMonthLetter(int month) {
        String monthLetter = "";
        switch (month) {
            case 1: monthLetter = "January"; break;
            case 2: monthLetter = "February"; break;
            case 3: monthLetter = "March";  break;
            case 4: monthLetter = "April";  break;
            case 5: monthLetter = "May";    break;
            case 6: monthLetter = "June";   break;
            case 7: monthLetter = "July";   break;
            case 8: monthLetter = "August"; break;
            case 9: monthLetter = "September";  break;
            case 10: monthLetter = "October";   break;
            case 11: monthLetter = "November";  break;
            case 12: monthLetter = "December";  break;
        }

        return monthLetter;
    }
    public static int perMonthDay(int currentYear, int month) {
        int n;
        switch (month) {
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                n = 31;
                break;
            case 2:
                if (month == 2) {
                    if (isLeapYear(currentYear))
                        n = 29;
                    else
                        n = 28;
                    break;
                }
            default: n = 30;
        }

        return n;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值