1

I am trying to format the String date, time and zone information.

LocalDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, YYYY hh:mm:ss a XXX"));
ZonedDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, YYYY hh:mm:ss a XXX"));

Please note for project support reasons i cannot use above java 8.

I am unable to get this parse work, I did try a lot of versions before i posted here. any support is appreciated.

Exception:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Dec 01, 2019 1:00:00 PM +00:00' could not be parsed at index 13
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:494)
    at com.parse.Test.main(Test.java:10)
Punith Raj
  • 2,164
  • 3
  • 27
  • 45

2 Answers2

4

Your date-time string, "Dec 01, 2019 1:00:00 PM +00:00" has timezone offset of 00:00 hours. The java.time API provides us with OffsetDateTime to parse this type of date-time string to.

Demo:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("MMM dd, uuuu h:mm:ss a XXX", Locale.ENGLISH);
        OffsetDateTime odt = OffsetDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", parser);
        System.out.println(odt);
    }
}

Output:

2019-12-01T13:00Z

ONLINE DEMO

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

What went wrong with your code?

  1. You have used hh whereas your string has only one digit in the hour.
  2. You have used Y instead of y. Note that Y is used for week-based-year. The right symbol for the intended purpose is y or u. Here, you can use y instead of u but I prefer u to y.

I also recommend you understand the importance of using a Locale while using date-time parsing/formatting API. Check Never use SimpleDateFormat or DateTimeFormatter without a Locale to learn more about it.

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

This Worked

LocalDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a XXX"));
ZonedDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a XXX"));

What I changed was:

  • We use lower case yyyy for the year. Format pattern strings are case sensitive.
  • We use just one h for the hour of day since it may be just one digit (1 in my example). A single h does accept two digits too, though, so times with 10, 11 or 12 will not pose any problem.
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Punith Raj
  • 2,164
  • 3
  • 27
  • 45
  • 1
    What is the difference to the code in your question? – Jens Jan 12 '23 at 17:16
  • Two further suggestions: (1) Prefer to parse into an `OffsetDateTime` rather then `LocalDateime` or `ZonedDateTime`. Your string contains an offset from UTC, `+00:00`, not a time zone like `Etc/UTC` or `Asia/Kolkata`. (2) Provide a locale. If `Dec` and `PM` are English, provide an English-speaking locale. Without it, if one day your code runs on a computer with a non-English default locale, it will crash. Use for example `DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a XXX", Locale.ENGLISH)`. – Ole V.V. Jan 12 '23 at 17:52