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 日语:金曜日 韩语:
原文:http://zl1030.blog.51cto.com/274507/1980326