Skip to content Skip to sidebar Skip to footer

Parsing A String To A Datetime Does Not Return The Correct Time

In Android...I am expecting 3:12 pm as time out put of the following code but I get 4:12 pm. Whats the correct way to parse this date time format. String dt = '2018-09-02T19:12:00-

Solution 1:

Time zone

It’s best to specify explicitly in which time zone you want your output:

DateTimeFormatterinputFormatter= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXX");
    DateTimeFormatterdisplayFormatter= DateTimeFormatter
            .ofLocalizedDateTime(FormatStyle.LONG)
            .withLocale(Locale.ENGLISH);
    ZoneIddisplayZone= ZoneId.of("Pacific/Pitcairn");

    Stringdt="2018-09-02T19:12:00-0400";
    OffsetDateTimedateTime= OffsetDateTime.parse(dt, inputFormatter);
    StringdisplayDateTime= dateTime.atZoneSameInstant(displayZone)
            .format(displayFormatter);
    System.out.println(displayDateTime);

This prints:

September 2, 2018 at 3:12:00 PM PST

I have used Pacific/Pitcairn time zone in my code, but you know better which time zone you want.

I am also using java.time, the modern Java date and time API. The date-time classes you are using, SimpleDateFormat and Date, are considered long outdated, and java.time is so much nicer to work with.

What went wrong in your code?

Your way of parsing your date string is correct and produces the correct Date.

When printing the Date, you are implicitly calling toString. The outdated Date class has a peculiar and confusing toString method: it grabs the JVM’s time zone setting and uses it for producing the string. So depending on your default time zone, you can get any hour of day in the output. So it seems your JVM’s time zone setting didn’t correspond to what you had expected.

Since you expected 3:12 PM from your input of 19:12:00-0400, I take it that you want a time zone that is at offset -08:00 from UTC in September. If for example your default time zone was America/Los_Angeles, the standard time of which is at -08:00, you would get Sun Sep 02 16:12:00 PDT 2018 because summer time (daylight saving time) is in effect in California in September, so the offset is -07:00.

Relying on your JVM’s default time zone is always fragile since the setting may be changed at any time by other parts of your program or by other programs running in the same JVM.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Post a Comment for "Parsing A String To A Datetime Does Not Return The Correct Time"