0

I am trying to parse a string of format

Sat Feb 01 15:00:19 AEDT 2014

into date object. My code looks like following:

SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
try{
    Date time = parserSDF.parse("Sat Feb 01 15:00:19 AEDT 2014");
}catch(Exception e){
    e.printStackTrace();
}

But I am getting a 'parse error'. I cannot change the input format of the date and I also cannot set my timezone to a static value as this code is to be run on andorid device. How can I parse this string into date ?

Eddard Stark
  • 3,575
  • 8
  • 35
  • 51

1 Answers1

1

"AEDT" is a 4-letter timezone which is not supported. That causes the exception. Only the valid timezones can be parsed.

You can use (GMT+11) instead of AEDT as shown below:

    SimpleDateFormat parserSDF = new SimpleDateFormat("E MMM dd HH:mm:ss yyyy");
    parserSDF.setTimeZone(TimeZone.getTimeZone("GMT+11"));
    try{
        Date time = parserSDF.parse("Sat Feb 01 15:00:19 2014");
    }catch(Exception e){
        Toast.makeText(this, "exception: "+e.toString(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
  • As this code is to be run in an android device, the time zone can vary and also apparently there are other 4 letter time zones. Is there any way to parse these string ? – Eddard Stark Aug 25 '14 at 11:30
  • @EddardStark: [Android doesn't allow](http://developer.android.com/reference/java/util/TimeZone.html) 4-letter time zones. If you have seen any 4-letter timezone being used, please share. Timezone won't vary on device because we set it through progmatically in above code - **parserSDF.setTimeZone**. If you want to get by region names, [you can get it using this solution](http://stackoverflow.com/a/24846843/2982225). – Infinite Recursion Aug 25 '14 at 11:55