22336

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)

Answer1:

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

...

Answer2:

Use new Date(millis); constructor of Date

new Date(1344855183166L);

Answer3:

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

Regards,

Answer4:

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


<hr>

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

    

Recommend

  • how to set Individual rows marked as non-draggable and/or non-droppable?
  • Returning array of int in C [duplicate]
  • What is the difference between == vs equals() in Java?
  • Parsed date has minute difference
  • how to check repeated elements in as string sequence/array?
  • how to play wav file in java 1.4
  • Java repetitive pattern matching
  • Windows xcopy using java Runtime.exec not working for filenames with chinese characters
  • Match and group each term and operator of expression using regex
  • Class to DataSet / DataSet to class
  • Does the Azure table storage API cache results?
  • What does certain JVM do after loading ByteCode into memory?
  • Get localized short date pattern as String?
  • vectorized indexing/slicing in numpy/scipy?
  • Do query loads all the data in memory
  • Tomcat memory Leak
  • PLSQL: Get number of records updated vs inserted when a merge statement is used
  • Scala multiline string placeholder
  • Zurb Foundation _global.scss meta styles for js?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • iOS: Detect app start via notification press
  • How to attach a node.js readable stream to a Sendgrid email?
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • QLineEdit password safety
  • Spray.io: When (not) to use non-blocking route handling?
  • MySQL WHERE-condition in procedure ignored
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Hazelcast - OperationTimeoutException
  • Web-crawler for facebook in python
  • QuartzCore.framework for Mono Develop
  • RestKit - RKRequestDelegate does not exist
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Revoking OAuth Access Token Results in 404 Not Found
  • trying to dynamically update Highchart column chart but series undefined
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • java string with new operator and a literal