Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

date

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)

like image 761
alicjasalamon Avatar asked Aug 13 '12 11:08

alicjasalamon


2 Answers

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);

...

like image 51
theINtoy Avatar answered Oct 30 '22 09:10

theINtoy


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

Instant.ofEpochMilli(millis).toString()
like image 29
Sridhar Sg Avatar answered Oct 30 '22 09:10

Sridhar Sg