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, and java.sql.Timestamp classes contain a static method called valueOf() which can also be used to parse simple date strings of the format: "yyyy-mm-dd". This is very useful for converting dates you might use in SQL strings while using JDBC, into Date objects.

These are useful techniques for converting user input date data into Java Date objects for further processing in your application. Your view can return user entered dates as strings, and using this technique, you can convert them to Date objects.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net