3

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.

Pravin Pundge
  • 66
  • 1
  • 6
  • 3
    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](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html). It has all parsing tools you need. – akuzminykh Mar 03 '20 at 20:53
  • [I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/). I don’t see any report of any here? – Ole V.V. Mar 03 '20 at 21:22
  • Does this answer your question? [How to compare two dates along with time in java](https://stackoverflow.com/questions/22039991/how-to-compare-two-dates-along-with-time-in-java) – MaThMaX Mar 04 '20 at 02:35
  • 1
    @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 04 '20 at 06:17

2 Answers2

7

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.

akuzminykh
  • 4,212
  • 4
  • 13
  • 34
  • 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 05 '21 at 14:44
  • Found the problem: A locale must be specified for some reason, credits to [this answer](https://stackoverflow.com/questions/4713825/how-to-parse-output-of-new-date-tostring#comment5234499_4713846) For me Locale.ROOT and Locale.US both worked (I'm from Germany). – wlfbck Aug 05 '21 at 15:07
  • 1
    @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 05 '21 at 17:41
2

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));
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
  • 1
    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,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Mar 03 '20 at 21:25