Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.sql.Date to 'dd-mm-yyyy' format

 
sandeep Kumar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can any one help me with the code snippet for the following?

I am getting a java.sql.Date object from the data base. I wanted it to be converted into the format of 'DD-MM-YYYY' the return object may be either a Date object or a String.

Thanks in Advance. Immediate response would be highly appreciated.

Regards,
Sandeep.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually if you want something that's formatted as dd-mmm-yyyy it would have to be a String. A Date object doesn't have any format, it's just a date.

Anyway to produce that String you should create a SimpleDateFormat object with the appropriate parameters, then call its format(Date) method on your java.sql.Date.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as Paul suggested SimpleDateFormat is the right solution to make your life easier.

see Java Doc to provide appropriate parameters in your SimpleDateFormat Object. or use Google to find an example.
 
sandeep Kumar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul Clapham, Muhammad Saifuddin

Thanks a lot for the response.

Regards,
Sandeep.
 
vinnu kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use SimpleDateFormat to set the format as you like..
DateFormat df=new SimpleDateFormat("dd-MMM-yyyy");
Date date=new Date();
String s=df.format(date);
System.out.println(s);
 
sandeep Kumar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinnu Kumar,

Thanks a lot for your response.

Regards,
Sandeep.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinnu, Welcome to ranch,

Suggestion : Use UBB Code specially when you pasting a code on forum, This is useful for displaying programming code.

Thanks,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic