6

I'm trying to parse the following string to a Date object:

2013-12-26T01:00:56.664Z

Using this SimpleDateFormat:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

But I'm getting a:

 java.text.ParseException: Unparseable date: "2013-12-26T01:00:56.664Z" (at offset 19)

What am I doing wrong, How I should handle the T and the Z letters in the date?

2
  • 1
    For new readers to this question use java.time, the modern Java date and time API, for your date and time work. See the answer by Arvind Kumar Avinash. SimpleDateFormat in its day was a notorious troublemaker of a class, so since it has been outdated for 10 years, you should never use it any more. Commented Nov 12, 2024 at 4:37
  • 1
    How I should handle the T and the Z letters in the date? You are handling T correctly by enclosing it in single quotes, as a literal; but you must never do that with Z. Z is a UTC offset (of zero) and must be parsed as an offset, or you get an incorrect result. It’s all correctly explained in the answer by Arvind Kumar Avinash. Commented Nov 12, 2024 at 4:43

3 Answers 3

5

The real isssue with the date is not T & Z but the milliseconds.

"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" This must be the format that is to be used becaue there are milli seconds as well in the input date.

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer was the first correct answer, so Thank you very much : )
This is not a correct answer, sorry. The trailing Z in the string to be parsed is a UTC offset of 0 and needs to be parsed as an offset. When instead you quote it as a literal in the format pattern string, you are getting the wrong result )in the vast majority of time zones).
2

java.time

In March 2014, Java 8 introduced the modern, java.time date-time API which supplanted the error-prone legacy java.util date-time API. Any new code should use the java.time API.

On a side note, never specify 'Z' in a date-time parsing/formatting pattern because 'Z' is a character literal while Z is a pattern character specifying time zone offset. To parse a string representing a time zone offset, you must use X (or XX or XXX depending on the requirement).

Solution using modern date-time API

Since your date-time string is in the default format used by Instant#parse, you can parse it to an Instant directly.

Demo:

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.parse("2013-12-26T01:00:56.664Z");
        System.out.println(instant);
    }
}

Output:

2013-12-26T01:00:56.664Z

Online Demo

Note: For whatever reason, if you need an instance of java.util.Date from this object of Instant, you can do so as follows:

java.util.Date date = Date.from(instant);

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

Comments

0

You can use this

String date = "2013-12-26T01:00:56.664Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
try {
   System.out.println(sdf.parse(date)); // Result Thu Dec 26 01:00:56 CET 2013
} catch (ParseException e) {
   e.printStackTrace();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.