Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert String to sql.Date

 
Ajay Tickle
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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:

[B]/
[/B]
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,
Ajay
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ajay
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic