6

Basically my date string is ISO-8601 date string, So I searched for converting ISO-8601 date string to date, but the solutions are lengthy. Here are the details.

I have a string 2018-02-02T06:54:57.744Z want to convert it to Date but I am getting error:

java.text.ParseException: Unparseable date: "2018-02-02T06:54:57.744Z"

I am using following technique to do that but no success:

SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
        parser.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date parsed = parser.parse(utcDateString);
        return parsed;

I also use following patterns with SimpleDateFormat but no success:

yyyy-MM-dd'T'HH:mm:ss.SSSZ
yyyy-MM-dd HH:mm:ss.S
yyyy-MM-dd'T'HH:mm:ssX
yyyy-MM-dd'T'HH:mm'Z'

any solution to this problem.

1
  • As an aside consider throwing away the long outmoded and notoriously troublesome SimpleDateFormat and friends, and adding ThreeTenABP to your Android project in order to use java.time, the modern Java date and time API. It is so much nicer to work with.
    – Anonymous
    Commented Feb 5, 2018 at 12:57

3 Answers 3

18

Z is timezone. You have to add timezone at the end of your date string or simply remove it from format: yyyy-MM-dd'T'HH:mm:ss

A correct date string with timezone is something like this: 2018-02-02T06:54:57.744+0200

1
  • 2
    Thanks using only yyyy-MM-dd'T'HH:mm:ss in pattern solve the problem. Commented Feb 3, 2018 at 7:26
17

Try this

SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");

Date d = null;
try 
{
   d = input.parse("2018-02-02T06:54:57.744Z");
} 
catch (ParseException e) 
{
   e.printStackTrace();
}
String formatted = output.format(d);
Log.i("DATE", "" + formatted);

OUTPUT

enter image description here

4
  • 2
    Thanks adding this line input.setTimeZone(TimeZone.getTimeZone("UTC")); give me correct result. Commented Feb 3, 2018 at 7:26
  • @Zulqarnain Thanks, Happy to help you. Commented Feb 3, 2018 at 7:28
  • Sir thanks for helping me out but Ali Behzadian Nejad answer first and just replacing the pattern to this yyyy-MM-dd'T'HH:mm:ss give me correct result. your answer was more in detail and correct as well I appreciate your time. Commented Feb 3, 2018 at 7:39
  • @Zulqarnain Thanks as you wish. Commented Feb 3, 2018 at 7:42
8

tl;dr

Instant.parse( “2018-02-02T06:54:57.744Z” ) 

java.time

The modern approach uses java.time classes.

Instant is the replacement for java.util.Date, representing a moment on the timeline in UTC.

Your input string happens to comply with the ISO 8601 standard. The java.time classes use standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.

Instant instant = Instant.parse( “2018-02-02T06:54:57.744Z” ) ;

Best to avoid the troublesome java.util.Date class entirely. But if you insist, convert using new methods added to the old classes.

Date d = Date.from( instant ) ;

For earlier Android, see the ThreeTen-Backport and ThreeTenABP projects.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

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