Parsing an ISO 8601 date format, e.g. 2015-06-27T13: 16: 37.363Z in Java

I am trying to parse a String using SimpleDateFormat .

This is my current code:

 public String getCreatedDateTime() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddEHH:mm:ss.zzzz"); try { Date date = simpleDateFormat.parse("2015-06-27T13:16:37.363Z"); return date.toString(); } catch (ParseException e) { return "Error parsing date"; } } 

As you can see, I just put the constant in the parse () method for testing purposes.

So here is what I am trying to parse:

2015-06-27T13: 16: 37.363Z

This is the SimpleDateFormat template that I am using:

YYYY-MM-ddEHH: mm: ss.zzzz

I keep getting ParseException.

I know this is possible because of .zzzz at the end, but I have no idea what .363Z could be, so I just used random letters. Bad idea.

I will be very grateful for your help. Thanks!

+6
source share
2 answers

Try this pattern (note the X at the end and the “T” in the middle):

 "yyyy-MM-dd'T'HH:mm:ss.SSSX" 

From the Java Documentation of SimpleDateFormat :

ISO 8601 Time Zone:

...

For parsing, “Z” is parsed as a UTC time zone designation.

And, from the part where he describes the different characters:

X - Time Zone - Time Zone ISO 8601

EDIT

If you are using Android, then "X" is not supported.

You can use this template (note Z is now literal):

 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" 

But then you will get the date in your current time zone and you will need to convert it to UTC if necessary.

+8
source

TL; DR

Skip formatting template. The standard ISO 8601 format is used by default.

 Instant.parse( "2015-06-27T13:16:37.363Z" ) 

ISO 8601

The format of your string is formally defined by the ISO 8601 standard .

Basically your question is a duplicate of this, Converting a string compatible with ISO 8601 to java.util.Date .

Alternatives

Eugenioy's answer is correct.

But you should know that the old java.util.Date/.Calendar/java.text.SimpleDateFormat classes bundled with Java are notorious and should be avoided.

Deprecated Classes

Those old classes are now deprecated, first the third - party Joda-Time library, and now the new java.time package ( Tutorial ), built into Java 8 and later (inspired by Joda-Time, defined by JSR 310 , extended by ThreeTen-Extra ).

Both java.time and Joda-Time use the ISO 8601 standard as default values ​​when parsing / generating string representations of date and time values. Thus, the code is simple; there is no need for custom formatting objects. No need to use all of this format that caused your exception.

Timezone

Both java.time and Joda-Time have a zoned time class that understands its assigned time zone (unlike java.util.Date). If you did not assign it, the current JVMs default time zone is assigned.

Beware that the current time zone of JVMs can change at any time. It can change during deployment, by default it does not depend on host settings. And it can change at any time during execution, when any code in any thread of any application in the JVM calls TimeZone.setDefault . Therefore, it is better to explicitly assign the desired / expected time zone.

java.time

Z at the end of the line is short for Zulu and means UTC. The Instant class can directly analyze this format to represent the moment on the timeline in UTC with a resolution in nanoseconds.

 String input = "2015-06-27T13:16:37.363Z"; Instant instant = Instant.parse( input ); 

Change the time zone from UTC to any desired / expected time zone.

 ZoneID zone = ZoneId.of( "America/Montreal" ) ; ZonedDateTime zdtMontréal = instant.atZone( zone ) ; 

If you really need java.util.Date to interact, convert.

 java.util.Date utilDate = Date.from( zdtMontréal.toInstant() ) ; 

Joda time

The Joda-Time project is now in maintenance mode, and the team advises switching to the java.time classes.

Sample code using Joda-Time 2.8.1.

 String input = "2015-06-27T13:16:37.363Z" ; DateTimeZone zone = DateTimeZone.UTC ; // Or: DateTimeZone.forID( "America/Montreal" ) ; DateTime dateTime = new DateTime( input, zone ) ; 

If you really need java.util.Date to interact, convert.

 java.util.Date date = dateTime.toDate(); 
+9
source

Source: https://habr.com/ru/post/989860/


All Articles