比较两个时间相差天数
1 不在意时间,只在意日期的。 比如 2014-12-31 23:59:59 和 2015-01-01 0:0:0 相差的是一天
/**
* 判断两个日期相差几天。
*/
public static int differDays(Date day1,Date day2){
if(day1 == null || day2 == null){
throw new RuntimeException("args is null");
}
return Days.daysBetween(new LocalDate(day1),new LocalDate(day2)).getDays();
}
2 需要在意时间的,两者的只有相差24个小时以上才会是相差一天,否则相差0天的
/**
* 判断两个日期相差几天。
*/
public static int differDays(Date day1,Date day2){
if(day1 == null || day2 == null){
throw new RuntimeException("args is null");
}
return Days.daysBetween(new DateTime(day1),new DateTime(day2)).getDays();
}
初始化时间
这个很简单,直接看代码
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dateTime = DateTime.parse("2014-09-08 19:59:59", format);
return dateTime.toDate();