File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Getting date as a string from util.date object in desired format format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting date as a string from util.date object in desired format format" Watch "Getting date as a string from util.date object in desired format format" New topic
Author

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

shuba karthik
Greenhorn

Joined: Sep 03, 2004
Posts: 14
Hi All,

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

TIA,
Shubakarthik
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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

Joined: Sep 03, 2004
Posts: 14
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Getting date as a string from util.date object in desired format format