添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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.

You can't because the second one is missing timezone information. If you assume a timezone for the second one then you can just use java.time . It has all parsing tools you need. akuzminykh Mar 3, 2020 at 20:53 I downvoted because research must be done to ask a good question . I don’t see any report of any here? Ole V.V. Mar 3, 2020 at 21:22 @MaThMaX Thanks for searching for similar questions, I’m sure there are more. This one is not an exact duplicate of the one you’re linking to, though: That one wants to compare to the time now (current time) and only has one date-time format, not two different ones. Ole V.V. Mar 4, 2020 at 6:17

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.

Sorry to say this, but your answer doesn't actually work: Exception in thread "main" java.time.format.DateTimeParseException: Text 'Wed Mar 04 03:34:45 GMT+08:00 2020' could not be parsed at index 0 – wlfbck Aug 5, 2021 at 14:44 Found the problem: A locale must be specified for some reason, credits to this answer For me Locale.ROOT and Locale.US both worked (I'm from Germany). – wlfbck Aug 5, 2021 at 15:07 @wlfbck The JVM uses the default locale of the OS, you can see it with Locale.getDefault(). A DateTimeFormatter will also use this locale as the default to parse inputs, i.e. it assumes that the inputs are in the locale's language. When your default is Locale.GERMANY, then it can't parse Wed. You can set the Locale for the formatter via DateTimeFormatter.ofPattern(...).withLocale(Locale.ENGLISH). You can also set the locale of the JVM via Locale.setDefault(...). – akuzminykh Aug 5, 2021 at 17:41

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));
                Please don’t teach the young ones to use the long outdated and notoriously troublesome SimpleDateFormat class. At least not as the first option. And not without any reservation. Today we have so much better in java.time, the modern Java date and time API, and its DateTimeFormatter.
– Ole V.V.
                Mar 3, 2020 at 21:25
  • 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

  • Convert the first Date-Time string into LocalDateTime after parsing and then compare it with the second Date-Time string parsed into a LocalDateTime.
  • 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.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.