alicjasalamon
alicjasalamon

Reputation: 4261

How to get ISO format from time in milliseconds in Java?

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)

Upvotes: 6

Views: 24188

Answers (6)

рüффп
рüффп

Reputation: 5438

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.

Upvotes: 4

Sridhar Sg
Sridhar Sg

Reputation: 1596

If you have epoch millis. The below line should work,

Instant.ofEpochMilli(millis).toString()

Upvotes: 5

Basil Bourque
Basil Bourque

Reputation: 338181

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

Upvotes: 3

theINtoy
theINtoy

Reputation: 3658

I thought you had asked how to get the time in this format "yyyy-MM-dd HH:mm:ss,SSS"

One way is to use java's SimpleDateFormat: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

NOTE that this is not thread-safe.

...

Date d = new Date(1344855183166L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
String dateStr = sdf.format(d);

...

Upvotes: 11

Y__
Y__

Reputation: 1777

The Date constructor does take a long (milliseconds) doesn't it?

Regards,

Upvotes: 3

Jigar Joshi
Jigar Joshi

Reputation: 240860

Use new Date(millis); constructor of Date

new Date(1344855183166L);

Upvotes: 2

Related Questions