簡體   English   中英

如何比較 java 格式“EEE MMM dd HH:mm:ss zzz yyyy”和“yyyy-MM-dd hh:mm:sss”的日期時間?

[英]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),您就可以執行兩者中的任何一個來比較它們

    1. 解析后將第一個日期時間字符串轉換為LocalDateTime ,然后將其與解析為LocalDateTime的第二個日期時間字符串進行比較。
    2. 解析后將第二個日期時間字符串轉換為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM