Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert unix epoch microseconds into Java time stamp

Tags:

java

unix

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 ?

like image 804
Ramesh Avatar asked Jul 11 '19 21:07

Ramesh


People also ask

How do I convert epoch time to readable time?

Convert from epoch to human-readable datemyString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch. =(A1 / 86400) + 25569 Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number).

What is Unix timestamp in Java?

1. Introduction. In this quick tutorial, we'll learn how to parse representations of dates from a Unix timestamp. Unix time is the number of seconds elapsed since January 1, 1970. However, a timestamp can represent time down to nanosecond precision.

How do I convert Unix time to normal time?

Simply multiply Unix timestamp by 1000 to convert it to a JavaScript time, because Unix timestamp measures time as a number of seconds, whereas in JavaScript time is fundamentally specified as the number of milliseconds (elapsed since January 1, 1970 at 00:00:00 UTC).

How is epoch timestamp calculated?

Epoch Time Difference FormulaMultiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.


1 Answers

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

like image 132
azurefrog Avatar answered Sep 22 '22 13:09

azurefrog