按日期比较 DateTime 对象
Compare DateTime objects by their dates
我希望对 Joda Time 非常了解的人可以帮助我。这是代码:
DateTimeComparator comparator = DateTimeComparator.getInstance(
DateTimeFieldType.dayOfMonth(), DateTimeFieldType.year());
java.sql.Date beforeDate = new java.sql.Date(1372910400000L); // July 4 2013
java.sql.Date nowDate = new java.sql.Date(System.currentTimeMillis());
DateTime beforeDateTime = new DateTime(beforeDate);
DateTime nowDateTime = new DateTime(nowDate);
int result = comparator.compare(nowDateTime, beforeDateTime);
System.out.println(nowDate+" compared to "+beforeDate+" = "+result);
2015-03-15 compared to 2013-07-04 = -1
来自 Javadocs API 进行比较:
Returns:
zero if order does not matter, negative value if lhsObj < rhsObj, positive value otherwise.
我做错了什么?
您似乎从比较中排除了年份,这使得 03/15 小于 07/04。
来自 getInstance(DateTimeFieldType lowerLimit,DateTimeFieldType upperLimit)
Returns a DateTimeComparator with a lower and upper limit. Fields of a
magnitude less than the lower limit are excluded from comparisons.
Fields of a magnitude greater than or equal to the upper limit are
also excluded from comparisons. Either limit may be specified as null,
which indicates an unbounded limit.
鉴于您当前的代码示例,您希望传递 null
作为 DateTimeComparator
的上限以按日期正确比较 DateTime
个对象:
DateTimeComparator.getInstance(DateTimeFieldType.dayOfMonth(), null);
这表示 "Compare DateTime
objects on the basis of the day of the month, and everything broader than that, treating more precise fields like time as equal"。
但实际上,这根本不是您想用 JodaTime 做的。 JodaTime 为不同的操作提供了多个不同的日期和时间类,如果你的目标是比较日期,你要使用LocalDate
。它的 .compareTo()
完全符合您的预期:
new LocalDate().compareTo(new LocalDate(2013,7,4)) // returns 1
请注意,java.sql.Date
仅存储日期信息,不存储时间或时区信息。通过构造一个 DateTime
对象,您人为地将时间和时区与日期相关联。 java.sql.Date
在 Joda-Time 中的正确兄弟是 LocalDate
.
当你关心日期时,使用 LocalDate
,当你关心时间时,使用 LocalTime
, when you care about times on a given day, use LocalDateTime
. What these classes all have in common is they represent our abstract notions of time, not an instant in history. This might seem strange, but it's often what you really want. The key-concepts documentation on partials 关于这个的一些细节。
只有当你真的需要实时谈论瞬间时,你才应该使用 DateTime
及其相关的 类。这实际上并不像您想象的 date/time-heavy 代码那样常见。从 Local*
类 开始,看看它们是否可以满足您的用例。
我希望对 Joda Time 非常了解的人可以帮助我。这是代码:
DateTimeComparator comparator = DateTimeComparator.getInstance(
DateTimeFieldType.dayOfMonth(), DateTimeFieldType.year());
java.sql.Date beforeDate = new java.sql.Date(1372910400000L); // July 4 2013
java.sql.Date nowDate = new java.sql.Date(System.currentTimeMillis());
DateTime beforeDateTime = new DateTime(beforeDate);
DateTime nowDateTime = new DateTime(nowDate);
int result = comparator.compare(nowDateTime, beforeDateTime);
System.out.println(nowDate+" compared to "+beforeDate+" = "+result);
2015-03-15 compared to 2013-07-04 = -1
来自 Javadocs API 进行比较:
Returns: zero if order does not matter, negative value if lhsObj < rhsObj, positive value otherwise.
我做错了什么?
您似乎从比较中排除了年份,这使得 03/15 小于 07/04。
来自 getInstance(DateTimeFieldType lowerLimit,DateTimeFieldType upperLimit)
Returns a DateTimeComparator with a lower and upper limit. Fields of a magnitude less than the lower limit are excluded from comparisons. Fields of a magnitude greater than or equal to the upper limit are also excluded from comparisons. Either limit may be specified as null, which indicates an unbounded limit.
鉴于您当前的代码示例,您希望传递 null
作为 DateTimeComparator
的上限以按日期正确比较 DateTime
个对象:
DateTimeComparator.getInstance(DateTimeFieldType.dayOfMonth(), null);
这表示 "Compare DateTime
objects on the basis of the day of the month, and everything broader than that, treating more precise fields like time as equal"。
但实际上,这根本不是您想用 JodaTime 做的。 JodaTime 为不同的操作提供了多个不同的日期和时间类,如果你的目标是比较日期,你要使用LocalDate
。它的 .compareTo()
完全符合您的预期:
new LocalDate().compareTo(new LocalDate(2013,7,4)) // returns 1
请注意,java.sql.Date
仅存储日期信息,不存储时间或时区信息。通过构造一个 DateTime
对象,您人为地将时间和时区与日期相关联。 java.sql.Date
在 Joda-Time 中的正确兄弟是 LocalDate
.
当你关心日期时,使用 LocalDate
,当你关心时间时,使用 LocalTime
, when you care about times on a given day, use LocalDateTime
. What these classes all have in common is they represent our abstract notions of time, not an instant in history. This might seem strange, but it's often what you really want. The key-concepts documentation on partials 关于这个的一些细节。
只有当你真的需要实时谈论瞬间时,你才应该使用 DateTime
及其相关的 类。这实际上并不像您想象的 date/time-heavy 代码那样常见。从 Local*
类 开始,看看它们是否可以满足您的用例。