Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to parse a string to a date??

Status
Not open for further replies.

Inandjo

Programmer
Dec 7, 2001
46
FR
hi,
am using jdk1.3.1, and i have a function to parse strings to date like this:

public Date stringToDate (String date) {

DateFormat df = DateFormat.getDateTimeInstance();
Date d = null;
try {
d = df.parse(date);
} catch (Exception e) {
.......
}
return d;
}

The date string was of the form "yyyy-mm-dd", but it's always throwing me an unparsable date exception!
What format should i then use for this string

La faim justifie les moyens!
 
You want to use SimpleDateFormat so you can set the format to your goal. The documentation for the class contains all the formatting syntax. [cheers]

-pete
 
Example :
Code:
  private void calcDate() {
    Date date = null;
    DateFormat myDate = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
    try {
      date = myDate.parse("2003-08-20");
    } catch (ParseException e) {
    }
    System.out.println("Date = " + date);
  }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top