5

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.

Zulqarnain Mustafa
  • 1,615
  • 2
  • 22
  • 25
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/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. – Ole V.V. Feb 05 '18 at 12:57

3 Answers3

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

Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
16

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

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154