File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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.
I agree. Here's the link:
subject: String to DATE
jQuery in Action, 3rd edition