How to convert the ISO8601 format to milliseconds?

I am very surprised that I have not yet found a very simple way, given how often ISO8601 is used in JSON.

Basically, I take a line that looks like this: 2014-10-23T00:35:14.800Z and turning it into something like 50 minutes ago .

Firstly, I need to change 2014-10-23T00:35:14.800Z to 2014-10-23'T'00:35:14.800Z , then I need to convert it to milliseconds, then it's easy.

My current code is:

 private void setTimestamp(String timeCreated) { int indexOfT = timeCreated.indexOf('T'); String properFormat = new StringBuilder(timeCreated).insert(indexOfT + 1, "'") .insert(indexOfT, "'") .toString(); timeStamp = (String) DateUtils.getRelativeTimeSpanString(Long.parseLong(properFormat), System.currentTimeMillis(), DateUtils.SECONDS_IN_MILLIS); } 

Criminal Long.parseLong(properFormat) . I need to convert properFormat to milliseconds.

+7
source share
2 answers

TL; DR

 Instant.parse( "2014-10-23T00:35:14.800Z" ) .toEpochMilli() 

One liner in java.time

The java.time framework is built into Java 8 and later. These new classes replace the older time classes associated with the earliest versions of Java, such as java.util.Date/.Calendar. See Tutorial . The java.time classes also supplant the very successful Joda-Time library, which is built by some of the same people, including those led by the same Stephen Colborn .

An Instant is a moment in the UTC timeline with a nanosecond resolution. You can ask him for the number of milliseconds from epoch (the first moment of 1970 in UTC ). But remember that Instant may have additional data, nanoseconds smaller than milliseconds. Thus, you can lose data in this small fraction of a second.

The java.time classes use standard ISO 8601 formats for parsing / generating strings. There is no need to specify a formatting template. The Instant class can directly parse a string .

 Instant.parse( "2014-10-23T00:35:14.800Z" ) 

You can convert this value to milliseconds from the first moment of 1970 in UTC by calling toEpochMilli

Be aware of potential data loss, as the Instant class can hold nanoseconds. Thus, extracting milliseconds will truncate any microseconds or nanoseconds in any fractional second. The line in your example contains only three digits in a fractional second, so this is just milliseconds. But six or nine digits of the decimal fraction will be truncated to three when they are converted to the number of milliseconds.

 long millisFromEpoch = Instant.parse( "2014-10-23T00:35:14.800Z" ).toEpochMilli(); 

To get the elapsed time in hours-minutes-seconds, use the Duration class. Feed it between method of a pair of points in time.

 Duration duration = Duration.between( Instant.parse( "2014-10-23T00:35:14.800Z" ) , Instant.now() ); 

One liner in Yoda time

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

With Joda-Time Library 2.5:

 long millisSinceEpoch = new DateTime( "2014-10-23T00:35:14.800Z" ).getMillis(); 

Joda-Time parses and generates ISO 8601 strings by default. Joda-Time runs on Android. The java.util.Date/.Calendar classes are known to be unpleasant, confusing, and erroneous. Avoid them.

+8
source

So it turns out that the answer was simpler than I could have imagined.

 private void setTimestamp(String timeCreated) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); try { Date timeCreatedDate = dateFormat.parse(timeCreated); timeStamp = (String) DateUtils.getRelativeTimeSpanString(timeCreatedDate.getTime(), System.currentTimeMillis(), DateUtils.SECONDS_IN_MILLIS); } catch ( ParseException e) {} } 

This will work.

+3
source

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


All Articles