![](/img/trans.png)
[英]Convert EEE MMM dd HH:mm:ss ZZZ yyyy to YYYY-MM-dd JAVA
[英]How to compare the datetime of format "EEE MMM dd HH:mm:ss zzz yyyy" and "yyyy-MM-dd hh:mm:sss" in java?
我的日期類型為“EEE MM DD HH:mm:ss zzz yyyy”(Wed Mar 04 03:34:45 GMT+08:00 2020)和“yyyy-MM-dd hh:mm:ss”(2020-02 -04 02:10:58).如何比較java中的這兩個日期?
這兩個日期都在同一時區。
如果您假設第二個日期的時區與第一個相同,那么您可以使用java.time 。 它擁有您需要的所有解析工具。 任何其他固定時區也適用。
下面是一個例子:
String a = "Wed Mar 04 03:34:45 GMT+08:00 2020";
String b = "2020-02-04 02:10:58";
ZonedDateTime parsedA;
ZonedDateTime parsedB;
DateTimeFormatter formatterA = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
parsedA = ZonedDateTime.parse(a, formatterA);
DateTimeFormatter formatterB = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
parsedB = LocalDateTime.parse(b, formatterB).atZone(parsedA.getZone());
// What do you want to compare? For example you can tell if a is after b.
System.out.println(parsedA.isAfter(parsedB));
如果您需要其他格式並需要模式字母和符號列表,請查看此處。
首先,這兩個日期不具有可比性,因為第二個日期缺少時區。
其次,如果您仍然想使用系統的默認時區來執行此操作,那么您需要將兩個日期都轉換為通用格式。
將日期解析為Date
對象,然后您可以玩轉它:
DateFormat dateFormat1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
Date date1 = dateFormat1.parse("Wed Mar 04 03:34:45 GMT+08:00 2020");
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date2 = dateFormat2.parse("2020-02-04 02:10:58");
System.out.println(date1.after(date2));
時區和時區偏移之間存在差異。 日期時間字符串 Wed Mar 04 03:34:45 GMT+08:00 2020 具有時區偏移量,而不是時區。 時區是唯一的,因此它有一個 ID,例如ZoneId.of("America/New_York")
,而時區偏移量告訴您給定時間與 UTC 時間的偏移量。 可以有多個時區落在同一時區偏移量上。 檢查List of tz database time zones以了解更多信息。 因此,最適合解析 Wed Mar 04 03:34:45 GMT+08:00 2020 的類型是OffsetDateTime
。
由於第二個日期時間字符串 2020-02-04 02:10:58 既沒有時區也沒有時區偏移量,因此將其解析為LocalDateTime
。
確保將Locale
與格式化程序一起使用,因為日期時間解析/格式化 API 是 Locale-sensitive 。
只要第二個日期時間字符串引用同一時區偏移量的日期時間(即 GMT+08:00),您就可以執行兩者中的任何一個來比較它們
LocalDateTime
,然后將其與解析為LocalDateTime
的第二個日期時間字符串進行比較。OffsetDateTime
,然后將其與解析為OffsetDateTime
的第一個日期時間字符串進行比較。我更喜歡第一種方法,因為它更簡單。 但是,為了完整起見,我在下面展示了這兩種方法。
第一種方法:
DateTimeFormatter odtFormatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss O uuuu", Locale.ENGLISH);
OffsetDateTime firstOffsetDateTime = OffsetDateTime.parse("Wed Mar 04 03:34:45 GMT+08:00 2020", odtFormatter);
LocalDateTime firstLocalDateTime = firstOffsetDateTime.toLocalDateTime();
DateTimeFormatter ldtFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
LocalDateTime secondLocalDateTime = LocalDateTime.parse("2020-02-04 02:10:58", ldtFormatter);
// Compare the two LocalDateTime values using isBefore, isAfter, equals etc.
if (firstLocalDateTime.isBefore(secondLocalDateTime)) {
// ...
}
第二種方法:
DateTimeFormatter odtFormatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss O uuuu", Locale.ENGLISH);
OffsetDateTime firstOffsetDateTime = OffsetDateTime.parse("Wed Mar 04 03:34:45 GMT+08:00 2020", odtFormatter);
DateTimeFormatter ldtFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
LocalDateTime secondLocalDateTime = LocalDateTime.parse("2020-02-04 02:10:58", ldtFormatter);
OffsetDateTime secondOffsetDateTime = secondLocalDateTime.atOffset(firstOffsetDateTime.getOffset());
// Compare the two OffsetDateTime values using isBefore, isAfter, equals etc.
if (firstOffsetDateTime.isBefore(secondOffsetDateTime)) {
// ...
}
與DateTimeFormatter
,我也更喜歡你。
從 Trail 了解有關現代日期時間 API的更多信息:日期時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.