Setix
Setix

Reputation: 141

Converting Microsecond time to Millisecond time

I am receiving a time-stamp from a GPS receiver and it is represented in microsecond time after seconds:

00:12:34.567891

Java 7 and below only use millisecond time, so I could split the string and parse the microsecond portion to millisecond, but that seems ridiculous.

Java 8 operates on nanosecond precision, but if I use a DateTimeFormatter with a pattern of "HH:mm:ss.nnnnnn", will the section after the "." be considered in terms of nanosecond?

Essentially how to I take my microsecond timestamp and convert it to a total number of milliseconds.

Upvotes: 2

Views: 6725

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338276

tl;dr

Duration.between( 
    LocalTime.MIN , 
    LocalTime.parse( "00:12:34.567891" )   
).toMillis()

See this code run live at IdeOne.com.

754567

java.time

Your input does not appear to be a GPS timestamp. You should determine and document an explanation of your data source.

The word “timestamp” usually means a point on the timeline, a date plus time-of-day plus time zone (or offset-from-UTC). In contrast, your 00:12:34.567891 appears to be a span of time, specifically twelve minutes, thirty-four seconds, and about a half second more (0.567891 seconds).

Assuming you will not have any input greater than 24 hours long, I suggest parsing as a LocalTime object.

String input = "00:12:34.567891" ;
LocalTime lt = LocalTime.parse( input );

Represent the span of time as a Duration.

Duration d = Duration.between( LocalTime.MIN , lt ) ;

Generate a String representing the value of this Duration in standard ISO 8601 format. In this format of PnYnMnDTnHnMnS where the P marks the beginning, and the T separates any years-months-days portion from any hours-minutes-seconds portion.

PT12M34.567891S

By the way, your input string value is a poor choice of format for serializing a span-of-time value. Use the standard ISO 8601 format instead whenever possible.

You seem to be asking for this entire span of time as a count of milliseconds. Be aware this means data-loss as any microseconds or nanoseconds in the fraction of a second are truncated to milliseconds.

long millis = d.toMillis() ;  // Consider entire span of time as milliseconds.

See this code run live at IdeOne.com.

754567


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?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Upvotes: 0

This can help you, is by default since Java7

TimeUnit.NANOSECONDS.convert(567891, TimeUnit.MILLISECONDS);

carefully give as parameter the time parsed as long..

Upvotes: 8

Related Questions