7

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 ?

1
  • You shouldn’t be using Strings and formats just to convert an Instant to a Date. Just use Date parsedDate = Date.from(instant);. Documentation here. Commented Jul 12, 2019 at 11:53

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yah, I was spacing between millis and micros for a bit, needed to update the math. I should know better than to post code without running it through a debugger first... :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.