This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes String to DATE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String to DATE" Watch "String to DATE" New topic

String to DATE

marys joseph

Joined: May 14, 2004
Posts: 20

I am reading data from excel file and updating the sql table. When I read all the contents from excel file are in String format. How do I change the string into Date.

For ex:

String s ='3/31/2003'

When I tried Date renew = new Date(s); it is not storing the correct date.

I need to insert 3/31/2003 into the DB table.

any suggestions?

Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Take a look at the api docs for java.text.SimpleDateFormat. It provides the mechanism for converting strings into date objects and vice-versa

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
The simplest way is...

Cut your String s into month, day and year and use the GregorianCalendar(int year, int month, int dayOfMonth) Constructor to create a GregorianCalendar object... Later get a date instance from it and then do things with DB...

Hope it helps...

Co-author of SCMAD Exam Guide, Author of JMADPlus
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
GregorianCalendar is nice EXCEPT that you have to be very careful with getting Dates & Timestamps you can lose some hours (this can affect Dates too). I second the DateFormat classes.
subject: String to DATE
It's not a secret anymore!