-3

Sorry for my ignorance but i can't get the correct result comparing ( sort from the newer to older ) multiple date in different time zone.

i get date from an xml server in this format :

Example :

Wed, 27 May 2020 20:39:02 +0400

Wed, 27 May 2020 19:12:22 +0200

Wed, 27 May 2020 15:31:32 GMT

I tried many solution in internet but always wrong results, thank you for your help

I tried to compare them one by one

public boolean isEarlier(String date1,String date2){
        DateFormat  format = new SimpleDateFormat("EEEE, dd MMMM yyyy kk:mm:ss Z");
        //final SimpleDateFormat formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy kk:mm:ss Z");
        //format.setTimeZone(TimeZone.getTimeZone("UTC"));
        //Date date = formatter.parse(date1);

        Date _date2 = null,_date1 = null;
        try {
             _date1 = format.parse(date1);
            _date2 = format.parse(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        if (_date1.compareTo(_date2) <= 0) {
            return true;
        }
        return false;
    }

but always wrong results

Singa
  • 351
  • 2
  • 11
  • 1
    Does this answer your question ? https://stackoverflow.com/questions/14451976/how-to-sort-date-which-is-in-string-format-in-java – Pluto May 27 '20 at 17:26
  • I tried it, see my edit question but always wrong results – Singa May 27 '20 at 17:35
  • 3
    why are you using the old cumbersome legacy date api instead of `java.time`? – Zabuzard May 27 '20 at 17:38
  • I can't see difference , i m not used to work with java time, sorry – Singa May 27 '20 at 17:42
  • I (too) recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `OffsetDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). `OffsetDateTime.parse("Wed, 27 May 2020 20:39:02 +0400", DateTimeFormatter.RFC_1123_DATE_TIME)` yields `2020-05-27T20:39:02+04:00` and works for your other strings too. – Ole V.V. May 28 '20 at 17:09
  • I cannot reproduce your problem. On my Java 8 your code parses your three example strings correctly. Which is your default locale? – Ole V.V. May 28 '20 at 17:18

1 Answers1

1

Refer to documentation for DateTimeFormatter class. Some of the symbols in your pattern aren't suitable for the sample strings you posted in your question.

Try the following:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss xx", Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse("Wed, 27 May 2020 20:39:02 +0400", formatter);

Wed is day of week in only three letters, hence EEE
The pattern also contains the hour in 24 hour format, hence HH rather than kk
Regarding the month, MMM is for three letter month, e.g. Apr (for April). If you want full name of month, add another M, i.e. MMMM.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • java.time.format.DateTimeParseException: Text 'Wed, 27 May 2020 13:50:30 GMT' could not be parsed at index 26 – Singa May 27 '20 at 17:59
  • 1
    @Singa You can't use the same format for both an offset and a named time zone. Refer to the documentation. Replace `xx` with `VV` for named time zone, like `GMT`. – Abra May 27 '20 at 18:04
  • Yes i understand, so i will try to change format first, then compare – Singa May 27 '20 at 18:05
  • Using java.time is a great suggestion. The format is RFC 1123 and the formatter is built-in. So I recommend you neither select nor count format patterns letters. Just use `DateTimeFormatter.RFC_1123_DATE_TIME`. – Ole V.V. May 28 '20 at 17:07