Date object capable of storing microseconds

I am looking for a Date object that can store up to microsecond granularity, does anyone know about it? The standard Date object stores only milliseconds (I know this is a platform limitation), and I could get around this by wrapping Date plus a fractional number in the user class. However, I was hoping to avoid having to write one with the appropriate calculations, etc. I need to parse boost::ptime label in Java and not lose accuracy ...

+4
source share
3 answers

JSR-310 is in alpha stage, but may be useful to you anyway. (Unfortunately, the predecessor of Joda Time only stores milliseconds.)

java.sql.Timestamp stores values ​​up to nanoseconds, but does not help in terms of calculations. What calculations do you need to do?

+6
source

java.time

The java.time classes support nanoseconds resolution, more than enough for microseconds .

The Instant class is the main building block class in java.time, representing the moment on the timeline in UTC with a resolution of up to nanoseconds. This means up to nine digits of decimal. Z at the end is short for Zulu and means UTC.

 Instant instant = Instant.parse( "2016-01-12T12:34:56.123456789Z" ); 

Please note that due to the obsolete implementation of Clock in Java 8, the current moment is fixed only up to a millisecond . Fixed in Java 9 with the new Clock implementation.

Line parsing

As for your mention of boost, ptime, call the boost std::string to_iso_extended_string(ptime) subroutine to generate a string in the standard ISO 8601 format. This string can be parsed as LocalDateTime , as the boost code does not include any offset information -from UTC or time zone.

 LocalDateTime ldt = LocalDateTime.parse( "2016-01-12T12:34:56.123456789" ) ; 

Parsing a long

If you have a few microseconds as a counter from an early 1970s epoch in UTC format ( 1970-01-01T00:00:00Z ), you can convert it to Instant . The Instant class represents a moment in the UTC timeline with a nanosecond resolution.

The Instant class has convenient static methods for converting from count whole seconds , from the number of whole seconds plus a fractional second in nanoseconds, or from the number of milliseconds . But there are no such methods for counting microseconds or nanoseconds.

As a workaround, we can define a Duration and add it to the reference date of an era already defined as a constant. We can create a Duration as the number of nanoseconds. To get nanoseconds, we multiply your microseconds by a thousand. Pay attention to the use of a 64-bit long , rather than a 32-bit int . Also note the L attached to the integer literal.

 long micros = 1_474_457_086_337_977L ; Duration duration = Duration.ofNanos( micros * 1_000L ); Instant instant = Instant.EPOCH.plus( duration ); 

instant.toString (): 2016-09-21T11: 24: 46.337977Z

+2
source

I do not know such a class in Java, but you can create a decorator class due to the Date and an additional field for storing microseconds and override / create several methods to take them into account.

My only contribution to this answer is to acknowledge your intuition and point to a sample decorator.

+1
source

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


All Articles