I am getting a Date format in String as Output like this.
Fri May 18 00:00:00 EDT 2012
I need to Convert this to a Date Object. What approach shall I use?
Thank you.
This is the program i used.
import java.util.*;
import java.text.*;
public class DateToString {
public static void main(String[] args) {
try {
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'EDT' yyyy ");
date = (Date)formatter.parse("Fri May 18 00:00:00 EDT 2012");
String s = formatter.format(date);
System.out.println("Today is " + s);
} catch (ParseException e) {
System.out.println("Exception :"+e);
}
}
}
Have a look at: java.text.SimpleDateFormat Java API
Update: note to self, locale can be important.
Use
SimpleDateFormat
with the following pattern:This doesn't worry about Timezone, Alternatively, with timezone inclusion: (untested)
EEE MMM dd HH:mm:ss z YYYY
(it's a lowercasez
). Bear in mind, I haven't tested it yet (as I'm on my way home from work).Use
SimpleDateFormat
and implementations to get a date displayable in a format you want.Example: