Ramesh
Ramesh

Reputation: 121

how to convert unix epoch microseconds into Java time stamp

Epoch time in MicroSeconds : 1529948813696000

how to convert this into java time stamp.

I am able to convert epoch time in MilliSeconds using this method

Instant instant = Instant.ofEpochMilli(Long.parseLong("1529957592000"));
Date parsedDate = dateFormat.parse(instant.atZone(ZoneId.of("America/Chicago")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")).toString())

Need help to convert Epoch time in Microseconds ?

Upvotes: 7

Views: 2368

Answers (1)

azurefrog
azurefrog

Reputation: 10955

While the Instant class doesn't have an ofEpochNano(long) method, it does has an overload of ofEpochSecond(long, long) that accepts a nanosecond offset along with the epoch seconds.

With a little math you can just convert your epoch microseconds to seconds plus an offset and create your Instant using it, like so:

long epochMicroSeconds = 1_529_948_813_696_123L;
long epochSeconds = epochMicroSeconds / 1_000_000L;
long nanoOffset = ( epochMicroSeconds % 1_000_000L ) * 1_000L ;
Instant instant = Instant.ofEpochSecond( epochSeconds, nanoOffset ) ;

See this code run live at IdeOne.com.

instant.toString(): 2018-06-25T17:46:53.696123Z

Upvotes: 7

Related Questions