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.
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
posted 12 years ago
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
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: