包含日期范围检查在Joda时间 [英] Inclusive Date Range check in Joda Time
问题描述
我正在使用Joda Time 2.1库。
如果给定的日期是在日期范围之间,我已经写了一个方法来比较。我希望它包含开始日期和结束日期。我使用 LocalDate
,因为我不想考虑时间部分只是日期部分。
以下是它的代码。
isDateBetweenRange(LocalDate start,LocalDate end, LocalDate target){
System.out.println(开始日期:
+ start.toDateTimeAtStartOfDay(DateTimeZone.forID(EST));
System.out.println(目标日期:
+ targettoDateTimeAtStartOfDay(DateTimeZone.forID(EST));
System.out.println(结束日期:
+ end.toDateTimeAtStartOfDay(DateTimeZone.forID(EST));
System.out.println(target.isAfter(start));
System.out。 println(target.isBefore(end));
}
上面的方法的输出是:
开始日期:2012-11-20T00:00:00.000-05:00
目标日期:2012- 11-20T00:00:00.000-05:00
结束D ate:2012-11-21T00:00:00.000-05:00
target.isAfter(start):false
target.isBefore(end):true
我的问题是 target.isAfter(start)
是假的,即使目标
日期和开始
具有相同的值。
我想要 target> = start
但这里它只考虑 target>开始
。
我想要它包含。
这是否意味着 isAfter
方法会专门查找匹配?
$ b $我已经经历了Joda Time的javadoc,但没有发现任何内容。
是的, isAfter
是排他的,否则应该可能已经命名为 isEqualOrAfter
或类似的东西。
解决方案:使用不在之前而不是之后而不是之前而不是之前。
code> boolean isBetweenInclusive(LocalDate start,LocalDate end,LocalDate target){
return!target.isBefore(start)&&& !target.isAfter(end);
}
I am using Joda Time 2.1 library.
I have written a method to compare if a given date is between a date range of not. I want it to be inclusive to the start date and end date.I have used LocalDate
as I don't want to consider the time part only date part.
Below is the code for it.
isDateBetweenRange(LocalDate start,LocalDate end,LocalDate target){
System.out.println("Start Date : "
+start.toDateTimeAtStartOfDay(DateTimeZone.forID("EST"));
System.out.println("Target Date : "
+targettoDateTimeAtStartOfDay(DateTimeZone.forID("EST"));
System.out.println("End Date : "
+end.toDateTimeAtStartOfDay(DateTimeZone.forID("EST"));
System.out.println(target.isAfter(start));
System.out.println(target.isBefore(end));
}
The output of above method is :
Start Date: 2012-11-20T00:00:00.000-05:00
Target Date: 2012-11-20T00:00:00.000-05:00
End Date : 2012-11-21T00:00:00.000-05:00
target.isAfter(start) : false
target.isBefore(end) : true
My problem is target.isAfter(start)
is false even if the target
date and start
are having the same values.
I want that target >= start
but here it considers only target > start
.
I want it inclusive.
Does it mean that isAfter
method finds a match exclusively ?
I have gone through the javadoc for Joda Time, but didn't found anything about it.
Yes, isAfter
is exclusive, otherwise it should probably have been named isEqualOrAfter
or something similar.
Solution: Use "not before" instead of "after", and "not after" instead of "before".
boolean isBetweenInclusive(LocalDate start, LocalDate end, LocalDate target) {
return !target.isBefore(start) && !target.isAfter(end);
}
这篇关于包含日期范围检查在Joda时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!