jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes string convert in to Sql ate format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "string convert in to Sql ate format" Watch "string convert in to Sql ate format" New topic
Author

string convert in to Sql ate format

reena Mehta
Greenhorn

Joined: Nov 17, 2010
Posts: 24
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

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317


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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
I think this discussion would sit better on our databases forum. Moving.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string convert in to Sql ate format
 
Similar Threads
What's wrong with my SimpleDateFormat?
Convert String to sql.Date
Inserting Date in database
Strange behavior with input parameter valuse during web service invocation
util.Date to sql.Date