tl;博士
Joda-Time 已被 java.time 类和 ThreeTen-Extra 项目取代。
代表时间跨度的LocalDateRange 和Interval 类使用半开定义。因此,询问开头是否包含返回 true。
LocalDateRange.of( // `org.threeten.extra.LocalDateRange` class represents a pair of `LocalDate` objects as a date range.
LocalDate.of( 2018, 8 , 2 ) , // `java.time.LocalDate` class represents a date-only value, without time-of-day and without time zone.
LocalDate.of( 2018 , 8 , 20 )
) // Returns a `LocalDateRange` object.
.contains(
LocalDate.now() // Capture the current date as seen in the wall-clock time used by the people of the JVM’s current default time zone.
)
是的
java.time
仅供参考,Joda-Time 项目现在位于maintenance mode,团队建议迁移到java.time 类。见Tutorial by Oracle。
仅限日期
显然您可能关心日期而不是一天中的时间。如果是这样,请使用LocalDate 类。
要管理日期范围,请将ThreeTen-Extra 库添加到您的项目中。这使您可以访问LocalDateRange 类。
该类提供了几种比较方法:abuts、contains、encloses、equals、intersection、isBefore、isAfter、isConnected、overlaps、@987654@3和union。
LocalDateRange r =
LocalDateRange.of(
LocalDate.of( 2018, 8 , 2 ) ,
LocalDate.of( 2018 , 8 , 20 )
)
;
LocalDate target = LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) ; // Capture the current date as seen in the wall-clock time used by the people of a particular time zone.
boolean contains = r.contains( target ) ;
日期时间
如果您关心特定时区的日期和时间,请使用ZonedDateTime 类。
从您的LocalDate 开始,让该班级决定一天的第一时间。由于夏令时 (DST) 等异常情况,这一天不总是从 00:00:00 开始。
以continent/region 的格式指定proper time zone name,例如America/Montreal、Africa/Casablanca 或Pacific/Auckland。切勿使用 3-4 个字母的缩写,例如 EST 或 IST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。
ZoneId z = ZoneId.of( "America/Montreal" ) ; // Or "America/New_York", etc.
ZonedDateTime zdtStart = LocalDate.of( 2018, 8 , 2 ).atStartOfDay( z ) ;
ZonedDateTime zdtStop = LocalDate.of( 2018, 8 , 20 ).atStartOfDay( z ) ;
ZonedDateTime zdtTarget = ZonedDateTime.now( z ) ;
用 ThreeTen-Extra 中的Interval 表示一个范围。此类表示一对Instant 对象。 Instant 是 UTC 中的时刻,始终是 UTC。只需提取Instant,我们就可以轻松地将我们的分区时刻调整为UTC。同一时刻,时间轴上的同一点,不同的挂钟时间。
Instant instantStart = zdtStart.toInstant() ;
Instant instantStop = zdtStop.toInstant() ;
Instant instantTarget = zdtTarget.toInstant() ;
Interval interval = Interval.of( instantStart , intervalStop ) ;
boolean contains = interval.contains( instantTarget ) ;
半开
定义时间跨度的最佳方法通常是半开放方法。这意味着开头是inclusive,而结尾是exclusive。
上面看到的ThreeTen-Extra 范围类(LocalDateRange 和Interval)中的比较都使用半开方法。因此,询问开始日期或开始时刻是否包含在范围内会导致true。
关于java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date、Calendar 和 SimpleDateFormat。
Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。
要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。
您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。
从哪里获得 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如Interval、YearWeek、YearQuarter 和more。