繁体   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