Parsing Strings into Dates

String dateString = "January 12, 1952 or 3:30:32pm";
											DateFormat df = DateFormat.getDateInstance();
											Date date = df.parse(dateString);

The DateFormat object is used to parse a String and obtain a java.util.Date object. Getting a DateFormat object using the getDateInstance() method will create a DateFormat object using the normal date format for your country. You can then use the parse() method of the returned DateFormat object to parse a date string into a Date object, as shown in the phrase.

The parse() method will also accept a second parameter. The second parameter specifies a parse position in the string to be parsed. This specifies a starting point in the string to begin the parsing at.

The java.sql.Date, java.sql.Time ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.