Is it simple way to get yyyy-MM-dd HH:mm:ss,SSS from time in millisecond? I've found some information how to do this from new Date() or Calendar.getInstance(), but couldn't find if it can be done from long (e.g. 1344855183166)
Is it simple way to get yyyy-MM-dd HH:mm:ss,SSS from time in millisecond? I've found some information how to do this from new Date() or Calendar.getInstance(), but couldn't find if it can be done from long (e.g. 1344855183166)
If you have epoch millis. The below line should work,
Instant.ofEpochMilli(millis).toString()
As said by Sridhar Sg's the code:
Instant.ofEpochMilli(millis).toString()
will work as the toString() method will give you the ISO-8601 extended format representation (with separators).
Note that the Instant class will only work from JDK 8 (introduction of the java.time package) unless you use ThreeTen Backport, the backport to Java 6 and 7.
If millis = 1603101879234 the above method will return: 2020-10-19T10:04:39.234Z
If you need other kind of format, like the ISO-8601 basic format (without separators except the T in the middle) you can use a custom DateTimeFormatter like this:
Instant instant = Instant.ofEpochMilli(millis);
DateTimeFormatter outFormatter = DateTimeFormatter
.ofPattern("yyyyMMdd'T'HHmmss.SSSX") // millisecond precision
.withZone(ZoneId.of("UTC"));
String basicIso = outFormatter.format(instant);
For the same millis = 1603101879234 the above method will yield: 20201019T100439.234Z.
The question does not mention time zone, so I'll assume you meant UTC/GMT. The question does not explain "ISO format", so I'll assume you meant ISO 8601. This happens to be the default for third-party Joda-Time 2.3 library. This is thread-safe.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
System.out.println( "That moment: " + new org.joda.time.DateTime( 1344855183166L, org.joda.time.DateTimeZone.UTC ) );
When run…
That moment: 2012-08-13T10:53:03.166Z
If the original poster meant a Poland time zone…
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// Time Zone list… http://joda-time.sourceforge.net/timezones.html
org.joda.time.DateTimeZone warsawTimeZone = org.joda.time.DateTimeZone.forID( "Europe/Warsaw" );
System.out.println( "That moment in Poland: " + new org.joda.time.DateTime( 1344855183166L, warsawTimeZone ) );
When run…
That moment in Poland: 2012-08-13T12:53:03.166+02:00