0

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); 
        }
    }
}
maksimov
  • 5,792
  • 1
  • 30
  • 38
Azar
  • 32
  • 1
  • 7

3 Answers3

3

Have a look at: java.text.SimpleDateFormat Java API

SimpleDateFormat dateParser = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", 
    Locale.US);
Date date = dateParser.parse("Fri May 18 00:00:00 EDT 2012");

Update: note to self, locale can be important.

Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
  • 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); } } } – Azar May 28 '12 at 16:05
  • Have to specify locale, I updated the answer with this earlier. – Mattias Isegran Bergander May 28 '12 at 16:07
  • Specify GMT timezone I think? – d1e May 28 '12 at 17:34
  • I can't vouch for your code as it is not doing the same thing as I have listed here, you have another format string in the code you pasted here @Azar ... You seem to have mixed it up with someones elses code and now accepted an answer that was made an hour later with my exact code :) – Mattias Isegran Bergander May 28 '12 at 19:02
0

Use SimpleDateFormat with the following pattern:

EEE MMM dd HH:mm:ss 'EDT' YYYY

This doesn't worry about Timezone, Alternatively, with timezone inclusion: (untested) EEE MMM dd HH:mm:ss z YYYY (it's a lowercase z). Bear in mind, I haven't tested it yet (as I'm on my way home from work).

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • That would parse `EDT` literally, and not as the timezone specifier, wouldn't it? – NPE May 28 '12 at 15:56
  • [code] 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); } } } – Azar May 28 '12 at 15:59
  • @aix, yes, that's because I was trying to figure out between `z` and `Z`. – Buhake Sindi May 28 '12 at 15:59
  • @user1422029, the comment section is **not** the place to paste code. Update your post, if you will. – Buhake Sindi May 28 '12 at 16:01
  • @user1422029 Please update your question with your code and tell us what exactly isn't working. Show exception stacktrace if applicable. – maksimov May 28 '12 at 16:11
  • @user1422029, please post the **stacktrace** if you want us to understand your error. Did you use my 2nd pattern (the one that include timezone)? – Buhake Sindi May 28 '12 at 16:43
  • ---------- java ---------- Exception :java.text.ParseException: Unparseable date: "Fri May 18 00:00:00 EDT 2012" – Azar May 28 '12 at 17:26
0

Use SimpleDateFormat and implementations to get a date displayable in a format you want.

Example:

String myDateString = "Fri May 18 00:00:00 EDT 2012";
SimpleDateFormat dateFormat = new SimpleDateFormat();
dateFormat.applyPattern( "EEE MMM dd HH:mm:ss z yyyy" );

try {
    Date d = dateFormat.parse( myDateString );
    System.out.println( d ); // Fri May 18 00:00:00 EDT 2012

    String datePattern1 = "yyyy-MM-dd";
    dateFormat.applyPattern( datePattern1 );
    System.out.println( dateFormat.format( d ) ); // 2012-05-18

    String datePattern2 = "yyyy.MM.dd G 'at' HH:mm:ss z";
    dateFormat.applyPattern( datePattern2 );
    System.out.println( dateFormat.format( d ) ); // 2012.05.18 AD at 00:00:00 EDT

    String datePattern3 = "yyyy.MM.dd G 'at' HH:mm:ss Z";
    dateFormat.applyPattern( datePattern3 );
    System.out.println( dateFormat.format( d ) ); // 2012.05.18 AD at 00:00:00 -400
}
catch ( Exception e ) { // ParseException
    e.printStackTrace();
}
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82