tl;博士
ZonedDateTime.parse( // Parse string into a date + time-of-day + time zone.
… , // Your input string.
DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss zzz uuuu" , Locale.US ) // Specify `Locale` to determine human language and cultural norms in parsing and translating the text.
)
.toLocalDate() // Extract the date-only portion of the `ZonedDateTime` object.
.isEqual(
LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) // Get current date as seen by people of a certain region (time zone).
)
java.time
Answer by aUserHimself 建议使用 jsoup 库是正确的。但是示例代码在其他方面是不明智的,会犯以下几个错误:
示例代码。
String input = … ;
Locale locale = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss zzz uuuu" , locale ) ;
ZonedDateTime zdt = ZonedDateTime.parse( input , f ) ;
LocalDate ld = zdt.toLocalDate() ;
与今天的日期进行比较。必须指定预期/期望的时区。对于任何给定的时刻,日期在世界各地因地区而异。例如,印度的新一天比加拿大早。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
Boolean isSameDate = ld.isEqual( today ) ;
关于java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date、Calendar 和 SimpleDateFormat。
Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。
要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。
使用符合JDBC 4.2 或更高版本的JDBC driver,您可以直接与您的数据库交换java.time 对象。不需要字符串或 java.sql.* 类。
从哪里获得 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如Interval、YearWeek、YearQuarter 和more。