Parsing custom formatted date string into Date object using SimpleDateFormat

September 13, 2012 0 Comments

This example shows how to parse string containing date and time in custom formats into Date object using Java SimpleDateFormat class.

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

public class ParsingCustomFormatDate {

public static void main(String[] args) {

//create object of SimpleDateFormat class with custom format
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");

try
{
//parse string containing specified format into date object
Date date = sdf.parse("11/10/14");
System.out.println(date);
}
catch(ParseException pe)
{ 
System.out.println("Parse Exception : " + pe);
}
  }
}

Prakash Hari Sharma
I'm a software developer and Java enthusiast. I like clean and modular code, enjoy Agile projects and have a passion for trying out new things. I try to learn and teach every day, and occasionally I'm even able to blog about it. :)