The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Dates

Posted by Yoo-Jin Lee on June 08, 2001 at 2:43 PM

Is there a generic way to parse any type of String into a date?
ie. Jan 1, 2000 or 01-01-2000 etc.

Using the SimpleDateFormat technique you'd have to use try/catch's to accept different formats.

Thanks for your help.
Yoo-Jin.


> Lets say you have a string representing a date in the format:
> String myDateStr = "2000-01-01"

> You define a Simple date format object in the same format:
> SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

> Have a look at the API for all of the masks you can set up for a SimpleDateFormat.

> Then, to parse your string into a date object, do the following:

> Date myDate = null;
> try {
> myDate = sdf.parse(myDateStr);
> } catch (ParseException pe) {
> System.out.println("Error parsing date");
> }

> Hope this helps,

> Regards,

> Jody






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us