How to compare the datetime of format "EEE MMM dd HH:mm:ss zzz yyyy" and "yyyy-MM-dd hh:mm:sss" in java?

huangapple go评论71阅读模式
英文:

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中如何比较这两个日期?这两个日期都在相同的时区。

英文:

I have date of type "EEE MM DD HH:mm:ss zzz yyyy" (Wed Mar 04 03:34:45 GMT+08:00 2020) and "yyyy-MM-dd hh:mm:ss" (2020-02-04 02:10:58).How to compare this two date in java?

Both dates are in same timezone.

答案1

得分: 7

如果您假设第二个日期的时区与第一个日期相同,那么您只需使用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());

// 您想要进行比较吗?例如,您可以判断 a 是否在 b 之后。
System.out.println(parsedA.isAfter(parsedB));

如果需要其他格式并且需要模式字符和符号的列表,请查看这里

英文:

If you assume that the timezone of the second date is the same as for the first one then you can just use java.time. It has all parsing tools you need. Any other fixed timezone works as well.

Here is an example:

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));

Have a look here if you need another format and need a listing of Pattern Letters and Symbols.

答案2

得分: 2

首先,这两个日期不可比较,因为第二个日期缺少时区信息。

其次,如果您仍希望使用系统的默认时区进行比较,您需要将这两个日期转换为相同的格式。

将日期解析为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));
英文:

First of all these two dates are not comparable because of missing timezone in the second date.

Secondly, If you still want to do that with system's default time zone then you need to bring both the dates into common format.

Parse the dates into Date object and then you can play around it:

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));

答案3

得分: 1

  • 时区和时区偏移之间存在差异。日期时间字符串“Wed Mar 04 03:34:45 GMT+08:00 2020”具有时区偏移,而不是时区。时区是唯一的,因此它有一个ID,例如ZoneId.of("America/New_York"),而时区偏移告诉您给定时间与UTC时间偏移的时间量。可以有许多时区与相同的时区偏移相对应。查看**tz数据库时区列表**以了解更多信息。因此,将“Wed Mar 04 03:34:45 GMT+08:00 2020”解析为最合适的类型是OffsetDateTime

  • 由于第二个日期时间字符串“2020-02-04 02:10:58”既没有时区也没有时区偏移,因此将其解析为LocalDateTime

  • 在使用格式化程序时,请务必使用Locale,因为日期时间解析/格式化API与Locale敏感

  • 只要第二个日期时间字符串指的是相同时区偏移(即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);

// 使用isBefore、isAfter、equals等比较两个LocalDateTime值。
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());

// 使用isBefore、isAfter、equals等比较两个OffsetDateTime值。
if (firstOffsetDateTime.isBefore(secondOffsetDateTime)) {
    // ...
}

我还更喜欢u而不是yDateTimeFormatter一起使用。

从**Trail: Date Time了解有关现代日期时间API**的更多信息。

英文:
  • There is a difference between time zone and time zone offset. The Date-Time string Wed Mar 04 03:34:45 GMT+08:00 2020 has a time zone offset, not a time zone. A time zone is unique and therefore it has an ID e.g. ZoneId.of("America/New_York") whereas a time zone offset tells you about the amount of time by which a given time is offset from the UTC time. There can be many time zones falling on the same time zone offset. Check List of tz database time zones to learn more about it. So, the most appropriate type to parse Wed Mar 04 03:34:45 GMT+08:00 2020 into is OffsetDateTime.

  • Since the second Date-Time string 2020-02-04 02:10:58 has neither a time zone nor a time zone offset, parse it into LocalDateTime.

  • Make sure to use Locale with the formatter because Date-Time parsing/formatting API is Locale-sensitive.

  • As long as the second Date-Time string refers to a Date-Time at the same timezone offset (i.e. GMT+08:00), you can do either of the two to compare them

    1. Convert the first Date-Time string into LocalDateTime after parsing and then compare it with the second Date-Time string parsed into a LocalDateTime.
    2. Convert the second Date-Time string into an OffsetDateTime after parsing and then compare it with the first Date-Time string parsed into an OffsetDateTime.

I would prefer the first approach as it is simpler. However, for the sake of completeness, I've shown below both approaches.

First approach:

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)) {
    // ...
}

Second approach:

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)) {
    // ...
}

I also prefer u to y with a DateTimeFormatter.

Learn more about the the modern date-time API from Trail: Date Time.

huangapple
  • 本文由 发表于 2020年3月4日 04:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/60515213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定