| 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
|
 |
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Getting date as a string from util.date object in desired format format
|
|
|