• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

string convert in to Sql ate format

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am trying to covert stringdate in to Sql date

stringDate is holding value 2008-03-19 19:44:58
#
# DateFormat formater = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
# java.util.Date parsedUtilDate = formater.parse(stringDate);
# java.sql.Date sqltDate= new java.sql.Date(parsedUtilDate.getTime());


output is: 2008-01-19
but i want same as 2008-03-19 19:44:58
please help me

Thanks
reena mehta

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly do you mean with "output is: 2008-01-19"? Where do you see that; somewhere in the database, where you're looking at what's in the database with some program? Or are you just doing System.out.println(sqltDate);?

If you're doing the latter: Date objects (java.util.Date as well as java.sql.Date) do not remember what format they are in. When you want to show them in a specific format, you have to format them yourself using, for example, a SimpleDateFormat object.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The line above does what you want. You get Date object from the String. I think you are then printing the date which prints in the default "yyyy-MM-dd" format.

----- sigh beaten by a quicker and better explanation.
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus 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 doesn't care about its time part, just as java.sql.Time doesn't care about its date part. Use java.sql.Timestamp if you need both.
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this discussion would sit better on our databases forum. Moving.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic