開源Java時間工具類Joda-Time體驗

import org.joda.time.*;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;

import java.util.Locale;

/**
 * @author by lei zhou on 2017/11/09 14:20.
 */
public class JodaTimeTest {

    @Test
    public void test() {


        // 日期輸出格式
        DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

        System.out.println("當前日期時間: " + DateTime.now().toString(dateTimeFormat));
        System.out.println("當前日期但時間清0: " + DateTime.now().withTimeAtStartOfDay().toString(dateTimeFormat));
        System.out.println("本月第一個週日的日期時間: " + getThisMonthFirstSunday().toString(dateTimeFormat));
        System.out.println("本週週一的日期時間: " + getThisWeekSunday().toString(dateTimeFormat));
        System.out.println("距離元旦天數: " + daysToNewYear(DateTime.now()));
        System.out.println("距離元旦月數: " + monthsToNewYear(DateTime.now()));
        System.out.println("當前月份最後一天日期: " +
                DateTime.now().withDayOfMonth(1)
                        .monthOfYear().addToCopy(1)
                        .dayOfMonth().addToCopy(-1)
                        .withTimeAtStartOfDay()
                        .toString(dateTimeFormat));

        String[] french = DateTimeUtils.getDateFormatSymbols(Locale.FRANCE).getWeekdays();
        String[] japanese = DateTimeUtils.getDateFormatSymbols(Locale.JAPAN).getWeekdays();
        String[] korean = DateTimeUtils.getDateFormatSymbols(Locale.KOREA).getWeekdays();

        System.out.println("今年每月第一天是周幾: ");
        for (int month = 1; month <= 12; month++) {
            DateTime monthDateTime = DateTime.now().withTimeAtStartOfDay().withMonthOfYear(month).withDayOfMonth(1);
            int index = monthDateTime.dayOfWeek().get() % 7 + 1;
            System.out.println(monthDateTime.toString(DateTimeFormat.fullDateTime()) + " 法語:" + french[index] + " 日語:" + japanese[index] + " 韓語:" + korean[index]);
        }

        DateTime birthday = DateTime.parse("1981-10-30 8:00:00", dateTimeFormat);
        System.out.println("距離出生已過去多少年: " + Years.yearsBetween(birthday, DateTime.now()).getYears());
        System.out.println("距離出生已過去多少天: " + Days.daysBetween(birthday, DateTime.now()).getDays());
        System.out.println("距離出生已過去多少分鐘: " + Minutes.minutesBetween(birthday, DateTime.now()).getMinutes());
    }

    private int monthsToNewYear(DateTime fromDate) {
        DateTime newYear = fromDate.plusYears(1).withDayOfYear(1);
        return Months.monthsBetween(fromDate, newYear).getMonths();
    }

    private int daysToNewYear(DateTime fromDate) {
        DateTime newYear = fromDate.plusYears(1).withDayOfYear(1);
        return Days.daysBetween(fromDate, newYear).getDays();
    }

    private DateTime getThisMonthFirstSunday() {
        return DateTime.now().withDayOfMonth(1).withDayOfWeek(DateTimeConstants.SUNDAY);
    }

    private DateTime getThisWeekSunday() {
        return DateTime.now().withDayOfWeek(DateTimeConstants.MONDAY);
    }
}

輸出結果:

當前日期時間: 2017-11-09 16:47:37
當前日期但時間清0: 2017-11-09 00:00:00
本月第一個週日的日期時間: 2017-11-05 16:47:37
本週週一的日期時間: 2017-11-06 16:47:37
距離元旦天數: 53
距離元旦月數: 1
當前月份最後一天日期: 2017-11-30 00:00:00
今年每月第一天是周幾: 
2017年1月1日 星期日 上午12時00分00秒 CST 法語:dimanche 日語:日曜日 韓語:
2017年2月1日 星期三 上午12時00分00秒 CST 法語:mercredi 日語:水曜日 韓語:
2017年3月1日 星期三 上午12時00分00秒 CST 法語:mercredi 日語:水曜日 韓語:
2017年4月1日 星期六 上午12時00分00秒 CST 法語:samedi 日語:土曜日 韓語:
2017年5月1日 星期一 上午12時00分00秒 CST 法語:lundi 日語:月曜日 韓語:
2017年6月1日 星期四 上午12時00分00秒 CST 法語:jeudi 日語:木曜日 韓語:
2017年7月1日 星期六 上午12時00分00秒 CST 法語:samedi 日語:土曜日 韓語:
2017年8月1日 星期二 上午12時00分00秒 CST 法語:mardi 日語:火曜日 韓語:
2017年9月1日 星期五 上午12時00分00秒 CST 法語:vendredi 日語:金曜日 韓語:
2017年10月1日 星期日 上午12時00分00秒 CST 法語:dimanche 日語:日曜日 韓語:
2017年11月1日 星期三 上午12時00分00秒 CST 法語:mercredi 日語:水曜日 韓語:
2017年12月1日 星期五 上午12時00分00秒 CST 法語:vendredi 日語:金曜日 韓語:
距離出生已過去多少年: 36
距離出生已過去多少天: 13159
距離出生已過去多少分鐘: 18949487


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章