| Author |
Convert String to Date format
|
Mahi Ranga
Ranch Hand
Joined: Jan 27, 2011
Posts: 33
|
|
Hi All,
I am facing one problem.that is convert String to Date Format.
my Programme is:
String dateString = "2011/03/28";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date convertedDate = dateFormat.parse(dateString);
System.out.println(" " + convertedDate);
output of this example is:Mon Mar 28 00:00:00 GMT+05:30 2011
but i want yyyy/MM/dd output.
Please help me.
Regards
Mahi Ranga.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2927
|
|
|
The best place to start is to read the JavaDatesFaq
|
Mohamed Sanaulla | My Blog
|
 |
Claudiu Chelemen
Ranch Hand
Joined: Mar 25, 2011
Posts: 65
|
|
Why don't you print your initial dateString variable?
Otherwise, you could use the same SimpleDateFormat object to format the output:
Cheers!
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12921
|
|
One important thing to understand is that Date objects by themselves do not have any format. So, if you parse a string into a Date object, using for example a SimpleDateFormat object with a certain format, then the resulting Date object will not somehow remember the format that the original string had.
If you print the content of a Date object, by simply printing it out like this:
then the date will be printed using some default format that looks like "Mon Mar 28 00:00:00 GMT+05:30 2011" (what you're seeing).
To print a date with a certain format, you'd have to convert it back to a string again. You can do that by calling the format() method of your SimpleDateFormat object. However, it doesn't make much sense to first parse the string into a Date object, and then doing exactly the reverse immediately. You could just as well just print the string directly and save the work of parsing and formatting it.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Chris Beckey
Ranch Hand
Joined: Jun 09, 2006
Posts: 116
|
|
Just to add to what Jesper has stated,
A java.util.Date is really just a long value (milliseconds since 01Jan1970 GMT, if memory serves correct), and does not carry any concept of calendar date in the way we usually think of a date. The string representations that you are seeing are produced by the default Locale and its associated Calendar and DateFormat instances. If you want some specific formatting, create a SimpleDateFormat and use its format() method or get the Locale's date formats and use those.
|
 |
 |
|
|
subject: Convert String to Date format
|
|
|