• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting date as a string from util.date object in desired format format

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

How can I get string from date object in <DD-MM-YYYY HH:MM:SS> format.

TIA,
Shubakarthik
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use parsing of Date Object after taking into in a string variable by DateObject.toString();
Then you can easily get the date format...

Otherwise you can also use DateFormat class in which you have to specify the Date format in which you want to convert.......
 
shuba karthik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see the code below

DateFormat startDateFormat = new SimpleDateFormat( "dd-MM-yyyy hh:mm:ss" ) ;
startDate = startDateFormat.parse( startDateNode.getText().trim() ) ;

I have the date object now.

Now I want to extract a string in "dd-MM-yyyy hh:mm:ss" this format from util.date object.

Thanks,
Subhakarthik
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String text = startDateFormat.format(startDate);

See the API documentation of java.text.SimpleDateFormat.

But what is the point, because you already have the date in text format:

startDate = startDateFormat.parse( startDateNode.getText().trim() ) ;

The expression: startDateNode.getText().trim() gives you the date in text format, then you parse it into a Date object. By calling format(), you do the opposite again (format the Date back into a String). You could just as well forget about the whole SimpleDateFormat object:

String text = startDateNode.getText().trim();
[ April 27, 2006: Message edited by: Jesper Young ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic