tags:

views:

718

answers:

4

I have a string representation of a date that I need to create an object from. I've looked through Date and Calendar API but haven't found anything that can do this other than creating my own ugly parse method. I know there must be a way, does anyone know of a solution?

A: 

DateFormat.parse(String)

Apocalisp
+13  A: 

In brief...

DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("01/29/02");

See http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html for more.

Matt Sheppard
A: 

The DateFormat class has a parse method.

http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html

Alexander Stolz
+8  A: 

The highly regarded Joda Time library is also worth a look. This is basis for the new date and time api that is pencilled in for Java 7. The design is neat, intuitive, well documented and avoids a lot of the clumsiness of the original java.util.Date / java.util.Calendar classes.

Joda's DateFormatter can parse a String to a Joda DateTime.

serg10