Java 计算JodaTime中两个日期之间的时间差
我使用的是Java 计算JodaTime中两个日期之间的时间差,java,jodatime,Java,Jodatime,我使用的是JodaTime,我需要检查两个日期是否在3个月的差异范围内。所以我写了一个简单的方法来检查这个 private boolean inRangeOf3Months(Pair<BusinessData, BusinessData> pair) { return pair.getKey().getDateTimeValue() != null && pair.getValue().getDateTimeValue() != null
JodaTime
,我需要检查两个日期是否在3个月的差异范围内。所以我写了一个简单的方法来检查这个
private boolean inRangeOf3Months(Pair<BusinessData, BusinessData> pair) {
return pair.getKey().getDateTimeValue() != null && pair.getValue().getDateTimeValue() != null ?
new Period(pair.getKey().getDateTimeValue(), pair.getValue().getDateTimeValue()).getMonths() <= 3 : false;
}
但那个不是,我将第一个日期设置为now()
,第二个日期设置为now()。plusMonths(3)。plusDays(1)
。所以它超出了我的范围,不应该被允许
@Test
public void shouldReturnFalseWhenOverRangeOf3Months() {
BusinessData closingDateFrom = businessData("closingDateFrom");
closingDateFrom.setDateTimeValue(DateTime.now());
BusinessData closingDateTo = businessData("closingDateTo");
closingDateTo.setDateTimeValue(DateTime.now().plusMonths(3).plusDays(1));
ReportingSearchCriteria criteria = criteriaOf(closingDateFrom, closingDateTo);
Assert.assertFalse(validator.isSufficient(criteria));
}
3个月和几天的时间仍然有3个月的差异。 因此,如果您希望将最后一个日期加上一天,并使用小于(而不是小于或等于):
新期间(pair.getKey().getDateTimeValue(),pair.getValue().getDateTimeValue().plusDays(1)).getMonths()<3
此外,请注意第二个日期应晚于第一个日期(否则它将不起作用)。3个月和几天的时间仍然有3个月的差异。 因此,如果您希望将最后一个日期加上一天,并使用小于(而不是小于或等于):
新期间(pair.getKey().getDateTimeValue(),pair.getValue().getDateTimeValue().plusDays(1)).getMonths()<3
另外,注意第二个日期应该是第一个日期(否则它将不起作用)。 < P >如果我理解你的比较必须在总的日子,而不是几个月,因为我们把今天的日期看作是第一个日期,第二个日期加上3个月加上25天。
DateTime date1 = DateTime.now();
System.out.println(date1);
DateTime date2 = DateTime.now().plusMonths(3).plusDays(25);
System.out.println(date2);
Period p = new Period(date1,date2);
System.out.println(p.getMonths());
输出
2019-08-09T04:31:07.400-05:00
2019-12-04T04:31:07.449-06:00
3
2019-08-09T04:34:29.530-05:00
2019-12-09T04:34:29.579-06:00
4
2019-08-09T04:40:47.334-05:00
2019-11-10T04:40:47.381-06:00
93
但是它返回的月差是3
,因为月差是3个月,25天是4个月还没有完成。现在,如果将第二个日期加上3个月加上30天,则返回的差值将为4
months(有时可能需要加上31天,因为这取决于有31天的月份)
输出
2019-08-09T04:31:07.400-05:00
2019-12-04T04:31:07.449-06:00
3
2019-08-09T04:34:29.530-05:00
2019-12-09T04:34:29.579-06:00
4
2019-08-09T04:40:47.334-05:00
2019-11-10T04:40:47.381-06:00
93
从上面的方法中,你甚至不能用两个月的时间来检查差异是否为3个月,因为在某些情况下,第二个日期可能返回第三个月(如上面所述,如果日期是月中,如果你再加上几天)
我建议比较总天数,例如,两天之间的总天数>90或>91。为此,您可以使用Duration
DateTime date1 = DateTime.now();
System.out.println(date1);
DateTime date2 = DateTime.now().plusMonths(3).plusDays(1);
System.out.println(date2);
Duration d = new Duration(date1, date2);
System.out.println(d.getStandardDays());
输出
2019-08-09T04:31:07.400-05:00
2019-12-04T04:31:07.449-06:00
3
2019-08-09T04:34:29.530-05:00
2019-12-09T04:34:29.579-06:00
4
2019-08-09T04:40:47.334-05:00
2019-11-10T04:40:47.381-06:00
93
如果我理解你的比较,必须在总的日子,而不是几个月,因为例如,让我们考虑今天的日期作为第一个日期和第二个日期,加上3个月加上25天< /P>
DateTime date1 = DateTime.now();
System.out.println(date1);
DateTime date2 = DateTime.now().plusMonths(3).plusDays(25);
System.out.println(date2);
Period p = new Period(date1,date2);
System.out.println(p.getMonths());
输出
2019-08-09T04:31:07.400-05:00
2019-12-04T04:31:07.449-06:00
3
2019-08-09T04:34:29.530-05:00
2019-12-09T04:34:29.579-06:00
4
2019-08-09T04:40:47.334-05:00
2019-11-10T04:40:47.381-06:00
93
但是它返回的月差是3
,因为月差是3个月,25天是4个月还没有完成。现在,如果将第二个日期加上3个月加上30天,则返回的差值将为4
months(有时可能需要加上31天,因为这取决于有31天的月份)
输出
2019-08-09T04:31:07.400-05:00
2019-12-04T04:31:07.449-06:00
3
2019-08-09T04:34:29.530-05:00
2019-12-09T04:34:29.579-06:00
4
2019-08-09T04:40:47.334-05:00
2019-11-10T04:40:47.381-06:00
93
从上面的方法中,你甚至不能用两个月的时间来检查差异是否为3个月,因为在某些情况下,第二个日期可能返回第三个月(如上面所述,如果日期是月中,如果你再加上几天)
我建议比较总天数,例如,两天之间的总天数>90或>91。为此,您可以使用Duration
DateTime date1 = DateTime.now();
System.out.println(date1);
DateTime date2 = DateTime.now().plusMonths(3).plusDays(1);
System.out.println(date2);
Duration d = new Duration(date1, date2);
System.out.println(d.getStandardDays());
输出
2019-08-09T04:31:07.400-05:00
2019-12-04T04:31:07.449-06:00
3
2019-08-09T04:34:29.530-05:00
2019-12-09T04:34:29.579-06:00
4
2019-08-09T04:40:47.334-05:00
2019-11-10T04:40:47.381-06:00
93
似乎最简单的方法就是将3个月后的分数(但是过去和未来)与另一个日期进行比较
public boolean areWithin3Months(DateTime dateTime1, DateTime dateTime2) {
return !dateTime1.minusMonths(3).isAfter(dateTime2)
&& !dateTime1.plusMonths(3).isBefore(dateTime2);
}
似乎最简单的方法就是将3个月后的分数(但是过去和未来)与另一个日期进行比较
public boolean areWithin3Months(DateTime dateTime1, DateTime dateTime2) {
return !dateTime1.minusMonths(3).isAfter(dateTime2)
&& !dateTime1.plusMonths(3).isBefore(dateTime2);
}