• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul Clapham, Muhammad Saifuddin

Thanks a lot for the response.

Regards,
Sandeep.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vinnu Kumar,

Thanks a lot for your response.

Regards,
Sandeep.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic