我只想比较两个org.joda.time.DateTime
值的日期,所以我使用了DateTimeComparator.getDateOnlyInstance()
- 但我看到了一些与它的 javadoc 不匹配的行为。我想知道这里是否有人可以解释一下。DateTimeComparator的javadoc关于其参数和返回值的说明如下:
Parameters:
lhsObj - the first object, logically on the left of a < comparison, null means now
rhsObj - the second object, logically on the right of a < comparison, null means now
Returns:
zero if order does not matter, negative value if lhsObj < rhsObj, positive value otherwise.
问题1:为什么会返回zero if order does not matter
。Comparator 接口的 javadoc中定义的此方法的合同规定返回值应为a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second
. (我突然想到这order does not matter
可能是作者说对象相等的方式,但这是一种令人困惑的方式。)
问题 2:考虑以下代码及其输出:
public static void main(String[] args) {
DateTime nowUTC = DateTime.now(DateTimeZone.UTC);
int comparisonResult = DateTimeComparator.getDateOnlyInstance().compare(null, nowUTC);
System.out.println("comparisonResult = " + comparisonResult);
}
输出(请记住null 表示 now,并且只应比较日期):
comparisonResult = -1
=== 阅读第一个答案和更多实验后的附加信息:
同意这意味着“现在处于默认系统时区”,但如果您比较andnull
的整个日期时间值,它们是相同的(请参阅下面的附加代码)。这对我来说似乎是正确的,因为现在在任何时区都是同一时刻。但是,各个字段可能不同。因此,昨天,我仅在 UTC 和 PST(我的默认系统时区)中的日期不同时才比较日期,我得到了预期的结果 = 1,因为 UTC 中的日期更大。此时,虽然两个区域的日期相同,但比较器似乎说 UTC 的日期小于PST 的日期,这是不可能的。DateTime.now(DateTimeZone.UTC)
null
测试代码:
公共静态无效主要(字符串[]参数){
System.out.println("Printing DateTime.now(DateTimeZone.UTC), and (DateTime)null using formatter.withZoneUTC()");
System.out.println("\tDateTime.now(DateTimeZone.UTC)\t= " +
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss-z").withZoneUTC().print(DateTime.now(DateTimeZone.UTC)));
System.out.println("\t(DateTime)null\t\t\t= " +
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss-z").withZoneUTC().print((DateTime)null));
System.out.println();
System.out.println("Printing (DateTime)null using formatter with default timezone");
System.out.println("\t(DateTime)null\t\t\t= " + DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss-z").print((DateTime)null));
System.out.println();
System.out.println("Comparing date only of DateTime.now(DateTimeZone.UTC) (LHS) and null (RHS)");
int dateOnlyComparisonResult = DateTimeComparator.getDateOnlyInstance().compare(DateTime.now(DateTimeZone.UTC), null);
System.out.println("\tdateOnlyComparisonResult\t= " + dateOnlyComparisonResult);
System.out.println();
System.out.println("Comparing entire date time value of DateTime.now(DateTimeZone.UTC) and null");
int instantComparisonResult = DateTimeComparator.getInstance().compare(DateTime.now(DateTimeZone.UTC), null);
System.out.println("\tinstantComparisonResult\t\t= " + instantComparisonResult);
}
输出:
Printing DateTime.now(DateTimeZone.UTC), and (DateTime)null using formatter.withZoneUTC()
DateTime.now(DateTimeZone.UTC) = 2016-02-11T19:26:07-UTC
(DateTime)null = 2016-02-11T19:26:08-UTC
Printing (DateTime)null using formatter with default timezone
(DateTime)null = 2016-02-11T11:26:08-PST
Comparing date only of DateTime.now(DateTimeZone.UTC) (LHS) and null (RHS)
dateOnlyComparisonResult = -1
Comparing entire date time value of DateTime.now(DateTimeZone.UTC) and null
instantComparisonResult = 0