Friday, April 20, 2007

Parse a string to Date and format a date to string

Parse a string to Date:

DateFormat parser = new SimpleDateFormat("dd-MMM-yy");
Date date = null;

try {
date = (Date) parser.parse("31-Jul-07");
System.out.println("date=" + date);//Tue Jul 31 00:00:00 CDT 2007
} catch (ParseException e) {
e.printStackTrace();
}


Format a date to string:

Format fmt = new SimpleDateFormat("yyyy-MM-dd");
String iso = fmt.format(date);
System.out.println("iso=" + iso);//2007-07-31

0 Comments:

Post a Comment

<< Home