Parsing a Date Using a Custom Format - Java Date Time

Java examples for Date Time:Legacy Date Parse

Description

Parsing a Date Using a Custom Format

Demo Code

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] args) throws Exception {
    try {/*from  www. j  a  v  a  2 s. c o m*/
      DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
      Date date = (Date) formatter.parse("01/29/02");
      System.out.println(date);
      formatter = new SimpleDateFormat("dd-MMM-yy");
      date = (Date) formatter.parse("29-Jan-02");
      System.out.println(date);
      formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
      date = (Date) formatter.parse("2002.01.29.08.36.33");
      System.out.println(date);
      formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
      date = (Date) formatter.parse("Tue, 29 Jan 2002 22:14:02 -0500");
      System.out.println(date);      
    } catch (ParseException e) {
    }
  }
}

Result


Related Tutorials