Threetenbp: Exception parse when parsing date with timezone name

I'm trying to parse dates in EEE format, dd MMM yyyy HH: mm: ss zzz, so for example strings like "Tue, May 16, 2017 07:44:48 GMT" using threeten DateTimeFormatter. However, it seems that the timezone name cannot be parsed for some reason (I tried to parse the same string only without the timezone name part and it worked).

Here is the syntactic part of the code:

DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
ZonedDateTime parsedDate = ZonedDateTime.parse(date, parseFormatter);

      

And I am getting the following error:

org.threeten.bp.format.DateTimeParseException: Text 'Tue, May 16, 2017 13:02:16 GMT' could not be parsed at index 26

I've tried all sorts of formats for the timezone part of the name (like z, zzz, Z, ZZZ) but nothing worked. Again, if I parse the subscript date without the timezone name part (in LocalDateTime) then it works, so I'm pretty sure the problem has something to do with the timezone name. Does anyone know what the problem is?

+3


source to share


2 answers


I don't know why your code was not working. This happens when I use classes java.time

in my Java 8. So, this is just speculation about a possible fix:

    DateTimeFormatter parseFormatter = DateTimeFormatter.RFC_1123_DATE_TIME;
    ZonedDateTime parsedDate = ZonedDateTime.parse(date, parseFormatter);

      

You will notice that this is a slight simplification on the same prong.

DateTimeFormatter.RFC_1123_DATE_TIME

documented as



Returns the RFC-1123 date format, for example, "Tue", June 3, 2008 11:05:30 GMT '.

So I believe it should accept GMT

as timezone name. I have to say that this matches your date string and this works on my computer too. I believe this formatter uses English abbreviations for the day of the week and month regardless of the language (or you can try it DateTimeFormatter.RFC_1123_DATE_TIME.withLocale(Locale.ENGLISH)

, but I really don't think it will be necessary).

However, they say that you should avoid the three and four letter acronyms. Some of them are ambiguous and some are not complete time zones, leading to further ambiguity. While GMT is not the most dangerous, a reliable solution to your problem would be if you can get a date string with an offset, for example +00:00

or simply Z

, instead of the name of the three letter zones.

See both examples from the Question and from this answer, works in real time at IdeOne.com . Both successes.

+3


source


TL; DR

When using the ThreeTen-Backport 1.3.4 Project Library on macOS (not Android):

  • I am not getting yours DateTimeParseException

    (parse succeeds)
  • But I am getting a different timezone than is assigned in the java.time classes built into Java 8.
    2017-05-16T13:02:16Z[Africa/Monrovia]

    and not2017-05-16T13:02:16Z[GMT]

Africa/Monrovia

zone?

Some of us have seen how your code works, obviously using the java.time classes built into Java 8.



But your question was specifically about the backport of these classes for Java 6 and Java 7. So I tried to use the following example using the ThreeTen-Backport project library.

package com.example.threetenbp.example;

import org.threeten.bp.*;
import org.threeten.bp.format.*;

import java.util.Locale;

/**
 * By Basil Bourque.
 */
public class App {
    public static void main ( String[] args ) {
        App app = new App ( );
        app.doIt ( );
    }

    private void doIt ( ) {
        String input = "Tue, 16 May 2017 13:02:16 GMT";
        DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern ( "EEE, dd MMM yyyy HH:mm:ss z" , Locale.ENGLISH );
        ZonedDateTime parsedDate = ZonedDateTime.parse ( input , parseFormatter );

        System.out.println ("parsedDate.toString(): " + parsedDate );
    }

}

      

I got curious results when building and running for Java 8 using IntellJ 2017.1.x on macOS Sierra 10.12.4. Instead of getting expected Z

or Z[GMT]

at the end, I got the time zone Africa/Monrovia

in Liberia. This is true since the UTC-offset is indeed zero (same as UTC / GMT). But this is not what we see with Java 8 Classes as shown in real time on IdeOne.com where we get 2017-05-16T13:02:16Z[GMT]

.

parsedDate.toString (): 2017-05-16T13: 02: 16Z [Africa / Monrovia]

I don't have a Java 6 or Java 7 implementation at my disposal. But I tried to set my build bytecode settings in IntelliJ to use Java 6. Same result.

+1


source







All Articles