Convert iso8601 date to timestamp unix in java

I have a date string

String s = "2014-09-01T19:22:43.000Z"; Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse(s); 

But I get an exception:

 Exception in thread "main" java.text.ParseException: Unparseable date: "2014-09-01T19:22:43.000Z" 

How to convert the above string to timestamp unix? Thanks

+6
source share
2 answers

X used for ISO 8601 time zone in SimpleDateFormat , not Z

The correct format is "yyyy-MM-dd'T'HH:mm:ss.SSSX"

+6
source

TL; DR

How to convert the above string to unix timestamp?

 Instant.parse( "2014-09-01T19:22:43.000Z" ) .getEpochSecond() 

java.time

The java.time.Instant class can parse your input string in the standard ISO 8601 format. There is no need to specify a formatting pattern.

 Instant instant = Instant.parse( "2014-09-01T19:22:43.000Z" ); 

To get a millisecond count from the 1970 era:

 long millisecondsSinceUnixEpoch = instant.toEpochMilli() ; 

Whole seconds from the era 1970-01-01T00: 00: 00Z:

 long secondsSinceUnixEpoch = instant.getEpochSecond() ; 

Know the possible loss of data when switching to milliseconds or whole seconds. The java.time classes have nanosecond resolution, so any microseconds or nanoseconds present in the value will be truncated.

Joda time

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

The Joda-Time library facilitates this work. The corresponding ISO 8601 string can be directly passed to the Joda-Time constructor. Built-in analyzer expects ISO 8601.

 DateTime dateTime = new DateTime( "2014-09-01T19:22:43.000Z" ) ; 

Unix Timestamp

What do you mean by Unix timestamp? Some people mean counting whole seconds from the first moment of 1970 UTC (Unix epoch ), ignoring the jump seconds (see Unix Time ). Some people mean milliseconds or another resolution.

Note the use of the long primitive, not the more general int .

For milliseconds, call getMillis() .

 long millisecondsSinceUnixEpoch = dateTime.getMillis(); 

Divide by 1000 for whole seconds. Think about whether you want to round or truncate fractional seconds.


I usually suggest passing a DateTimeZone object along with your string to the DateTime constructor. But it is not necessary if all you need is an account from the era.


About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

+6
source

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


All Articles