0

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.

Community
  • 1
  • 1
user1033698
  • 89
  • 1
  • 7
  • Look at the "Related" section at the right of this page, and you'll find dozens of similar questions. – JB Nizet Jun 20 '12 at 11:38

5 Answers5

2

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.

dbf
  • 6,399
  • 2
  • 38
  • 65
  • 1
    There are known concurrency issues with SimpleDateFormat. FastDateFormat is essentially the same only threadsafe – John Kane Jun 20 '12 at 11:39
1

FastDateFormat is (from their api), "a fast and thread-safe version of SimpleDateFormat." Here is an article that explains it

John Kane
  • 4,383
  • 1
  • 24
  • 42
1

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);
Community
  • 1
  • 1
Asif
  • 4,980
  • 8
  • 38
  • 53
1
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
Ahmad
  • 2,110
  • 5
  • 26
  • 36
1

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);
Jesper
  • 202,709
  • 46
  • 318
  • 350