<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${last.version}</version>
</dependency>
当前时间
// 当前时间
DateTime.now();
// 当前小时
DateTime.now().getHourOfDay();
//年
DateTime.now().getYear();
//月
DateTime.now().getMonthOfYear();
//日
DateTime.now().getDayOfMonth();
//星期
DateTime.now().getDayOfWeek();
//点
DateTime.now().getHourOfDay();
//分
DateTime.now().getMinuteOfHour();
//秒
DateTime.now().getSecondOfMinute();
//毫秒
DateTime.now().getMillisOfSecond();
格式化
String nowH = DateTime.now().toString("yyyyMMddHHmm");
DateTimeFormatter yyyyMMdd= DateTimeFormat.forPattern("yyyyMMdd");
DateTime dateTime = DateTime.parse("20191122", yyyyMMdd);
Datedate = new Date();
DateTime dateTime = new DateTime(date); datTime.toString("yyyy-MM-dd HH:mm:ss");
Date 转 DateTime
new DateTime(new Date())
获取零点
DateTime currentDateTime = new DateTime();
// 今天的零点
DateTime dateTime = currentDateTime.withMillisOfDay(0);
// 昨天的零点
DateTime dateTime = currentDateTime.withMillisOfDay(0).plusDays(-1);
// 明天的零点
DateTime dateTime = currentDateTime.withMillisOfDay(0).plusDays(1);
// 这一年最后一天0点
new DateTime().dayOfYear().withMaximumValue().withMillisOfDay(0)
// 这一年第一天0点
new DateTime().dayOfYear().withMinimumValue().withMillisOfDay(0)
// 这个月最后一天0点
new DateTime().dayOfMonth().withMaximumValue().withMillisOfDay(0)
// 这个月月初0点
new DateTime().dayOfMonth().withMinimumValue().withMillisOfDay(0)
获取相差
public static void main(String[] args) {
DateTime begin = new DateTime();
DateTime end = new DateTime().plusDays(2);
// 相差多少年
int years = Years.yearsBetween(begin, end).getYears();
System.out.println("years:"+years);
// 相差多少月
int months = Months.monthsBetween(begin, end).getMonths();
System.out.println("months:"+months);
// 距离国庆放假还有多少天,嘎嘎!
int days = Days.daysBetween(begin, end).getDays();
System.out.println("days:"+days);
// 相差多少小时
int hours = Hours.hoursBetween(begin, end).getHours();
System.out.println("hours:"+hours);
// 相差多少分钟
int minutes = Minutes.minutesBetween(begin, end).getMinutes();
System.out.println("minutes:"+minutes);
// 相差多少秒
int seconds = Seconds.secondsBetween(begin, end).getSeconds();
System.out.println();
// 相差多少周
int weeks = Weeks.weeksBetween(begin, end).getWeeks();
System.out.println();
}
日期加减
// 增加日期 天/小时/分钟/秒/周/年
DateTime dateTime = DateTime.parse("2021-09-30");
dateTime = dateTime.plusDays(1);
dateTime = dateTime.plusHours(2);
dateTime = dateTime.plusMinutes(3);
dateTime = dateTime.plusMonths(4);
dateTime = dateTime.plusSeconds(5);
dateTime = dateTime.plusWeeks(6);
dateTime = dateTime.plusYears(7);
// 减少日期 毫秒/小时/秒/
DateTime dateTime = DateTime.parse("2021-09-30");
dateTime = dateTime.minusMillis(1);
dateTime = dateTime.minusHours(1);
dateTime = dateTime.minusSeconds(1);;
判断是否闰月
DateTime time = new DateTime();
org.joda.time.DateTime.Property month = time.monthOfYear();
System.out.println("是否闰月:" + month.isLeap());
计算时间差
/**
* 计算时间差
* @param beginTime 开始时间
* @param endTime 结束时间
* @param beginTimeFormat 开始时间格式
* @param endTimeFormat 结束时间格式
* @param returnTime 输出类型 0:天,1:小时,2:分钟,3:秒,4:毫秒
*/
public static Long timeDifference(String beginTime, TimeFormatEnum beginTimeFormat,
String endTime, TimeFormatEnum endTimeFormat,
Integer returnTime) {
DateTimeFormatter beginFormatter = DateTimeFormat.forPattern(beginTimeFormat.getFormat());
DateTimeFormatter endTFormatter = DateTimeFormat.forPattern(endTimeFormat.getFormat());
DateTime begin = DateTime.parse(beginTime, beginFormatter);
DateTime end = DateTime.parse(endTime, endTFormatter);
// 计算区间毫秒数
Duration etime = new Duration(begin, end);
switch (returnTime) {
case 0:
return etime.getStandardDays();
case 1:
return etime.getStandardHours();
case 2:
return etime.getStandardMinutes();
case 3:
return etime.getStandardSeconds();
default:
return etime.getMillis();
}
}