Java Data Type How to - Preserve milliseconds in conversion with simple date format








Question

We would like to know how to preserve milliseconds in conversion with simple date format.

Answer

import java.text.SimpleDateFormat;
import java.util.Date;
/*from  w  w w  .  ja va 2s .c  o m*/
public class Main {
  public static void main(String[] args) throws Exception {
    long yourmilliseconds = 1119193123;
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss.SSS");

    Date resultdate = new Date(yourmilliseconds);
    System.out.println(sdf.format(resultdate));
  }
}

The code above generates the following result.