Java Data Type How to - SimpleDateFormat format: 30-MAR-2012








Question

We would like to know how to simpleDateFormat format: 30-MAR-2012.

Answer

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
//ww w.j  av a 2s.co  m
public class Main {

  public static void main(String[] args) throws Exception {
    Date birth = null;
    String birthDate = "30-MAR-2012";
    DateFormat formatter = null;
    formatter = new SimpleDateFormat("dd-MMM-yyyy");
    birth = (Date) formatter.parse(birthDate); // birtDate is a string
    if (birth == null) {
      System.out.println("Birth object is still null.");
    } else {
      System.out.println("Default date format " + birth);
      System.out.println("Our SimpleDateFormat " + formatter.format(birth));
      System.out.println("Our SimpleDateFormat with all uppercase "
          + formatter.format(birth).toUpperCase());
    }
  }
}

The code above generates the following result.