• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert String to sql.Date

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I AM MIGHTY! Especially when I hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic