开发者

A Date object capable of storing microseconds

I'm looking for a Date object capable of storing down to microsecond granularity, does anyone know of one? The standard Date object only stores down to milliseconds (I know that this is a platform limitation) and I could get around this by wrapping a Date plus a fractional quantity in a custom class. However I was hoping to avoid having to write one开发者_如何学Go with the appropriate calculations etc. I need to parse a boost::ptime timestamp in Java and not loose the precision...


JSR-310 is in alpha stage, but may be useful to you anyway. (Unfortunately it's predecessor, Joda Time, only stores as far as milliseconds.)

java.sql.Timestamp stores values down to nanoseconds, but doesn't help in terms of calculations. What calculations do you need to perform?


java.time

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

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

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

Note that due to a legacy implementation of Clock, in Java 8 the current moment is captured only up to milliseconds. Remedied in Java 9 with a fresh implementation of Clock.

Parsing a string

As for your mention of "boost" "ptime", call the boost routine std::string to_iso_extended_string(ptime) to generate a String in standard ISO 8601 format. That string can be parsed as a LocalDateTime since the boost code neglects to include any offset-from-UTC or time zone info.

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

Parsing a long

If you happen to have a number of microseconds as a count from the epoch of beginning of 1970 in UTC (1970-01-01T00:00:00Z), you can convert that into an Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds.

The Instant class has handy static methods for converting from a count of whole seconds, from a count of whole seconds plus a fractional second in nanoseconds, or from a count of milliseconds. But no such methods for a count of microseconds or nanoseconds.

As a workaround, we can define a Duration and add it to the epoch reference date already defined as a constant. We can instantiate a Duration as a number of nanoseconds. To get nanoseconds, we multiply your microseconds by a thousand. Note the use of 64-bit long rather than 32-bit int. Also note the L appended to 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


I don't know such a class in Java but you could create a decorator class owing a Date and an additional field to store microseconds and override/create a few methods to take them into account.

My only contribution in this answer is to acknowledge your intuition and point you to the decorator pattern.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜