2

Here is code to parser "Wed, 01 Jul 2015 17:32:41 EDT", but it still doesn't work. Does anyone can help me figure it out? many thanks.

   SimpleDateFormat formatter = new SimpleDateFormat("EEE dd MMM yyyy HH:mm:ss zzz");
    try {
        return formatter.parse(text);
    } catch (ParseException e) {
        e.printStackTrace();
    }
Psypher
  • 10,717
  • 12
  • 59
  • 83
NickTseng
  • 61
  • 1
  • 4

4 Answers4

1

Try this

public static void main(String args[]) {
        SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
    try {
        System.out.println(formatter.parse("Wed, 01 July 2015 17:32:41 EDT"));
    } catch (Exception e) {
        e.printStackTrace();
    }}

Output : Thu Jul 02 03:02:41 IST 2015

KDP
  • 1,481
  • 7
  • 13
0

You have a , after Wed... You need to account for that as well in the format...

Use "EEE, dd MMM yyyy HH:mm:ss zzz"

Codebender
  • 14,221
  • 7
  • 48
  • 85
  • here is exception : java.text.ParseException: Unparseable date: "Thu, 02 Jul 2015 02:28:39 EDT" (at offset 0) – NickTseng Jul 02 '15 at 08:17
  • Did it give the error even after adding the comma??? It is working perfectly fine for me... – Codebender Jul 02 '15 at 08:18
  • I think it should be `"EEE',' dd MMM yyyy HH:mm:ss zzz"` i.e. add `'` around the comma as it it's not a recognised data symbol – Carlo Moretti Jul 02 '15 at 08:23
  • @Onheiron... It doesn't make a difference... Special characters like comma, colon, space don't require quote around them... – Codebender Jul 02 '15 at 08:42
0

Use comma AND locale (Thu is english):

new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
0

Try this.

    try {
        String s = "Wed, 01 Jul 2015 17:32:41 EDT";
        SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); //Or Locale.ENGLISH
        df.setTimeZone(TimeZone.getTimeZone("EDT"));

        //Or set timeZone to GMT-4:00 because EDT is GMT-4:00
        //df.setTimeZone(TimeZone.getTimeZone("GMT-04:00"));

        Date time = null;
        time = df.parse(s);
        System.out.println("Time = " + s);
        String parsed = df.format(time);

        } catch (Exception e) {
        e.printStackTrace();
    }

Output is:

Old = Wed, 01 Jul 2015 17:32:41 EDT

Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54