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 Convert String to sql.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 "Convert String to sql.Date" Watch "Convert String to sql.Date" New topic

Convert String to sql.Date

Ajay Tickle

Joined: Mar 16, 2008
Posts: 17

In my application I am getting one String variable which holds date in yyyy-mm-dd hh:mm:ss format. It is a String type and i would like to convert it in sql.Date. TO convert in sql.Date type i am doing the following:

1) I am getting value in format of yyyy-mm-dd hh:mm:ss, example 2008-03-19 19:44:58
2) i am trying like:

when i convert String date i get in parsedUtilDate: Wed Mar 19 19:44:58 IST 2008
and sqltDate is having value 2008-03-19

could you please help me to get date in same format in sqltDate (as it is stringDate), 2008-03-19 19:44:58

thank you for your help,
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
sqltDate is a Date object - it has no formatting. If you want to format it, use a DateFormatter like formater, which already has the format you're looking for. Its format format can transform a Date to a String. But since stringDate already has that string value, I'm not sure why you'd want to do this.
Ajay Tickle

Joined: Mar 16, 2008
Posts: 17
Hi Ulf,

Thanks for your early reply.

i need to insert sqltDate in database through hibernate. i want to convert as 2008-03-19 19:44:58 is converted to Wed Mar 19 19:44:58 IST 2008 in java.util.Date variable

As you mentioned Its format format can transform a Date to a String.

i want to convert string to java.sql.Date so sqltDate variable could hold the 2008-03-19 19:44:58 and it could be inserted in database field which has datatype Date.

Is there any way to convert string value 2008-03-19 19:44:58 to java.sql.Date.

Thank you for help and reply
Paul Clapham

Joined: Oct 14, 2005
Posts: 19869

Originally posted by ajay tickle:
Is there any way to convert string value 2008-03-19 19:44:58 to java.sql.Date.
The code you posted with your original question already does that.

Don't be misled by what you see when you use System.out.println to display a java.util.Date or java.sql.Date object. What you see there is just for your convenience and has nothing to do with how Hibernate will use the object.

Are you having actual problems with Hibernate or is this just a problem that you think you have, so you haven't tried using it with Hibernate yet?
Lisa Li

Joined: Nov 07, 2008
Posts: 1
Hi Ajay,

Have you eventually converted the string to sqltDate (as it is stringDate), 2008-03-19 19:44:58?

I am having the same problem (the sqlDate only has the date information, lost the time infor after conversion) as you had a while ago. If you were able to solve the problem, could you let me know how you did it?

Your help is greatly appreciated.

Thank you!
Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

java.sql.Date is a type for dates only, just as java.sql.Time is a type for times only. To combine them use java.sql.Timestamp.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Convert String to sql.Date
It's not a secret anymore!