File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sql date convert

 
raj baig
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
raj baig
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
raj baig
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"... 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic