Java Data Type How to - Convert date and time format








Question

We would like to know how to convert date and time format.

Answer

import java.text.SimpleDateFormat;
import java.util.Date;
/*from w  w w.java2 s  .c  om*/
public class Main {
  public static void main(String[] args) throws Exception {
    SimpleDateFormat sd = new SimpleDateFormat();
    String dateString = "2015-01-02 14:59:27.953";
    sd.applyPattern("yyyy-MM-dd HH:mm:ss.SSS");
    Date date = sd.parse(dateString);
    sd.applyPattern("MM/dd/yyyy HH:mm a");
    System.out.println(sd.format(date));
  }
}

The code above generates the following result.