Sunday, July 18, 2010

How to convert a String to date object in Java

I am going to illustrate using an example

import java.util.Date;
import java.text.SimpleDateFormat;
public class test{
public static void main(String args[]){
String dateStr = "26/08/2010";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

try{
Date date = formatter.parse(dateStr);
System.out.println("Date is : " + formatter.format(date));
}
catch (java.text.ParseException e){
e.printStackTrace();
}

}
}

No comments:

Post a Comment