41

I get the datetime value as

created_at  '2012-02-22T02:06:58.147Z'
Read-only. The time at which this task was created.

Which is given by Asana API

I am using Java 8 to parse the date time as following

import java.time.*;
import java.time.format.*;

public class Times {

  public static void main(String[] args) {
    final String dateTime = "2012-02-22T02:06:58.147Z";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss.SX");


    final ZonedDateTime parsed = ZonedDateTime.parse(dateTime, formatter);
    System.out.println(parsed);
  }
}

When I run this, I get the following error

Exception in thread "main" java.time.format.DateTimeParseException: Text '2012-02-22T02:06:58.147Z' could not be parsed at index 21
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
    at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
    at Times.main(Times.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

What is not right here?

1
  • You might want to change to "yyyy-M-d' to handle single digit months and days.
    – paiego
    Nov 28, 2017 at 22:05

5 Answers 5

36

The default parser can parse your input. So you don't need a custom formatter and

String dateTime = "2012-02-22T02:06:58.147Z";
ZonedDateTime d = ZonedDateTime.parse(dateTime);

works as expected.

28

If your input always has a time zone of "zulu" ("Z" = UTC), then you can use DateTimeFormatter.ISO_INSTANT (implicitly):

final Instant parsed = Instant.parse(dateTime);

If time zone varies and has the form of "+01:00" or "+01:00:00" (when not "Z"), then you can use DateTimeFormatter.ISO_OFFSET_DATE_TIME:

DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
final ZonedDateTime parsed = ZonedDateTime.parse(dateTime, formatter);

If neither is the case, you can construct a DateTimeFormatter in the same manner as DateTimeFormatter.ISO_OFFSET_DATE_TIME is constructed.


Your current pattern has several problems:

  • not using strict mode (ResolverStyle.STRICT);
  • using yyyy instead of uuuu (yyyy will not work in strict mode);
  • using 12-hour hh instead of 24-hour HH;
  • using only one digit S for fractional seconds, but input has three.
2
  • Good observation regarding strict or lenient mode! I had originally assumed lenient mode in my answer. For information: Pattern with only one S-symbol instead of ".SSS" is okay in lenient mode. Mar 9, 2016 at 3:20
  • Saved by "format "+01:00" or "+01:00:00" (when not "Z"), then you can use DateTimeFormatter.ISO_OFFSET_DATE_TIME"
    – UserBSS1
    Jun 3, 2021 at 16:14
2

Your original problem was wrong pattern symbol "h" which stands for the clock hour (range 1-12). In this case, the am-pm-information is missing. Better, use the pattern symbol "H" instead (hour of day in range 0-23). So the pattern should rather have been like:

uuuu-MM-dd'T'HH:mm:ss.SSSX (best pattern also suitable for strict mode)

1

The following worked for me

import java.time.*;
import java.time.format.*;

public class Times {

  public static void main(String[] args) {
    final String dateTime = "2012-02-22T02:06:58.147Z";
    DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
    final ZonedDateTime parsed = ZonedDateTime.parse(dateTime, formatter.withZone(ZoneId.of("UTC")));
    System.out.println(parsed.toLocalDateTime());
  }
}

and gave me output as

2012-02-22T02:06:58.147
1

java.time API is based on ISO 8601 and therefore you do not need a DateTimeFormatter to parse a date-time string which is already in ISO 8601 format (e.g. 2012-02-22T02:06:58.147Z).

What's wrong with your pattern?

There are two problems with your pattern, yyyy-MM-dd'T'hh:mm:ss.SX:

  1. It has h which is used for AM/PM time and therefore it is the wrong symbol for an hour in 24-hour format. The symbol for an hour in 24-hour format is H.
  2. Your date-time string has a fraction of a second in milliseconds resolution but you have used only one S. To match the resolution of milliseconds, the pattern should be SSS.

Demo:

class Main {
    public static void main(String args[]) {
        String strDateTime = "2012-02-22T02:06:58.147Z";
        System.out.println(Instant.parse(strDateTime));
        System.out.println(ZonedDateTime.parse(strDateTime));
        System.out.println(OffsetDateTime.parse(strDateTime));

        // Parsing with your pattern after correction
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
        System.out.println(ZonedDateTime.parse(strDateTime, formatter));
    }
}

Output:

2012-02-22T02:06:58.147Z
2012-02-22T02:06:58.147Z
2012-02-22T02:06:58.147Z
2012-02-22T02:06:58.147Z

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.