標籤:java
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())); 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]); } } 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:01:36當前日期但時間清0: 2017-11-09 00:00:00本月第一個周日的日期時間: 2017-11-05 16:01:36本周周一的日期時間: 2017-11-06 16:01:36距離元旦天數: 53距離元旦月數: 1今年每月第一天是周幾: 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 日語:金曜日 韓語:
本文出自 “zl1030的記錄” 部落格,請務必保留此出處http://zl1030.blog.51cto.com/274507/1980326
開源Java時間工具類Joda-Time體驗