0

I am constantly getting an exception error in my code when parsing a date. The Date looks like this:

Wed May 21 00:00:00 EDT 2008

This is the code for trying to read it:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
Property property = new Property();
property.setSale_date(LocalDateTime.parse(linesplit[8], formatter));

Expected result: A LocalDateTime of 2008-05-21T00:00.

What I got instead:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Wed May 21 00:00:00 EDT 2008' could not be parsed at index 0

Am I doing this completely wrong?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
wnxxion
  • 29
  • 1

2 Answers2

1

The devil is in the detail. You are basically doing it correctly, except:

  • You need to provide a locale for the formatter.
  • By parsing into a LocalDateTime you are losing information and possibly getting an unexpected result.

So I suggest:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
            "EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);

    String linesplit8 = "Wed May 21 00:00:00 EDT 2008";
    ZonedDateTime zdt = ZonedDateTime.parse(linesplit8, formatter);

    System.out.println(zdt);

Output is:

2008-05-21T00:00-04:00[America/New_York]

Your string is in English. It looks like the output from Date.toString() (Date being the old-fashioned class used for times before Java 8). So it’s probably in English because that toString method always produced English output. So if your locale is not an English-speaking one, parsing is deemed to fail, and I believe this is the reason why it did. In this case it’s appropriate to use Locale.ROOT for the locale neutral English-speaking locale. A way to say “don’t apply any locale specific processing here”.

Your string contains a time zone abbreviation, EDT, which is part of identifying a unique point in time, so you will want to pick up this part of the information too. Therefore use a ZonedDateTime.

Links

There are some related/similar questions, for example:

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1
  • Avoid using the three-letter time zone ID. Given below is an extract from as old as Java 6 documentation:

Three-letter time zone IDs

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz uuuu", Locale.ENGLISH);
    
ZonedDateTime zdt = ZonedDateTime.parse("Wed May 21 00:00:00 EDT 2008", formatter);
System.out.println(zdt);

Output:

2008-05-21T00:00-04:00[America/New_York]

Let's try this formatter for the same date-time string replacing EDT with the recommended America/New_York:

ZonedDateTime zdt = ZonedDateTime.parse("Wed May 21 00:00:00 America/New_York 2008", formatter);
System.out.println(zdt);

The output remains same.

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

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110