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?
+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
2008-09-04 13:56:37
A:
The DateFormat class has a parse method.
http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html
Alexander Stolz
2008-09-04 13:57:40
+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
2008-09-04 15:28:11