Possible Duplicate:
Parsing a date in Java?
Java string to date conversion
How can i parse Date object which by default returns this format:
Nov 22, 1963 00:00:00 PM
to this format:
Fri Nov 22 1963 00:00:00 GMT-0600
Thanks for help.
Possible Duplicate:
Parsing a date in Java?
Java string to date conversion
How can i parse Date object which by default returns this format:
Nov 22, 1963 00:00:00 PM
to this format:
Fri Nov 22 1963 00:00:00 GMT-0600
Thanks for help.
Use SimpleDateFormat ( http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html ) class to parse original string, snd after it format it using new format with SimpleDateFormat class.
FastDateFormat is (from their api), "a fast and thread-safe version of SimpleDateFormat." Here is an article that explains it
First you need to have a Date
object from String
for this you can use SimpleDateFormat
, Here is an Answer related to this step.
Secondly, you have to get back this Date
in respective text format, for this again use SimpleDateFormat
as:
Format formatter = new new SimpleDateFormat("your format here");
String dateString = formatter.format(date);
SimpleDateFormat sdfDestination = new SimpleDateFormat("E MMM-dd-yyyy hh:mm:ss");
sdfDestination .setTimeZone(TimeZone.getTimeZone("GMT-6").getID());
String strDate = sdfDestination.format(oldDateObject); //pass here your old date object
The way you phrase your question indicates that you're confusing a number of things.
First of all, you don't "parse a Date object". You can convert a string that contains a date (as text) into a Date
object. That's what is called parsing. So, you parse a string and the result is a Date
object - you don't "parse a Date object".
The opposite (converting a Date
object into a string) is called formatting.
Date
objects by themselves do not have a format. Just like a number is just a number (it doesn't have an inherent format), a Date
is just a date. When you call toString()
(explicitly or implicitly) on a Date
object, then the date will be printed using some default format that looks like Nov 22, 1963 00:00:00 PM
. But the format is not a property of the Date
object itself.
To display a Date
in a specific format, you can use a DateFormat
object, to format the date into a string which can then be printed. You specify the exact format in the DateFormat
object. For example:
Date date = new Date();
DateFormat df = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z");
String text = df.format(date);
System.out.println(text);