File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes sql date convert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "sql date convert" Watch "sql date convert" New topic
Author

sql date convert

raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
Hi,
I have a string contaings date.
String date="10-04-2007";
now i am converting it into sql date using simpledateformat's parse method
which returns me sql date but in different format.
ie. 2006-01-10

But i want the returned date objected should follow same format like
10-04-2007.

Can you assist me.

thangs in advance
raj.
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
What is the format string you have used in your SimpleDateFormat?
This is probably the problem here, in this case it should look something like "dd-MM-yyyy"
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Are you concerned about java.sql.Date.toString()? That always returns the same string format, per the JavaDoc. You can use your date formatter to get it to another string format.
[ April 19, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
Hi,
i want to convert a string in sql or util date object to insert in oracle database but in the same format .

String date="10-04-2006";
SimpleDateFormat df1=new SimpleDateFormat("dd-MM-yyyy");
Date d=df1.parse(date);
java.sql.Date sqlDate = new java.sql.Date ( t ) ;
System.out.println("date:"+sqlDate);


Here originally my string format is : dd-mm-yy 10-04-2006
Sql date returns after parsing : yyyy-mm-dd. 2006-04-10


Please tell me how to convert it.

----
Raj.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Raj,

You have to understand that Date objects (doesn't matter if it's java.util.Date or java.sql.Date) do not have a format by themselves. A Date object is just an object that contains information about a specific point in time. It does not contain other information, such as information about a format.

When you convert a Date object to a String by formatting it, or when you convert a String to a Date object by parsing it, then you need to specify how the String (should) look - that is what you use a format string for.

If you call toString() on a Date object, it will use a default format. The API documentation for the method java.sql.Date.toString() says:

"Formats a date in the date escape format yyyy-mm-dd."

So the default format for a java.sql.Dat object is: yyyy-MM-dd. So that is what you will see if you call toString() on a java.sql.Date object, which is what you are (implicitly) doing in the code in your last post.

If you want the java.sql.Date in a particular format, you will have to format it using that particular format. For example:


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

Joined: Jul 11, 2006
Posts: 96
Hi Jesper Young

The format method will return a string. But i want the string as date object.

because i have to set the date object to a java bean ,this date object
contains the format dd-mm-yyyy to store in db as db take the format
dd-mm-yyyy.

private date.
public setDate(java.sql.Date date){
thid.date=date
}

Awaiting for reply,
Raj.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

"... because i have to set the date object to a java bean ,this date object
contains the format dd-mm-yyyy
to store in db as db take the format
dd-mm-yyyy.


Read my post above again. Date objects do not contain a format.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sql date convert
 
Similar Threads
Convert to timestamp yyyy-MM-dd hh:mm:ss a
How to check a date is within range of dates ?
Date/SimpleDateFormat help need
hql query to get records of the week
comparing dates in hibernate