ParseException - unable to determine the correct template

I have the following line: dateToParse = "Fri May 16 23:59:59 BRT 2014"and want to parse it using DateFormat:

DateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Sao_Paulo"));
cal.setTime(dateFormat.parse(dateToParse));

I'm trying with help right now pattern = "EEE MMM dd HH:mm:ss z yyyy", but getting this exception:

java.text.ParseException: Unparseable date: "Fri May 16 23:59:59 BRT 2014" (at offset 0)

I can’t understand what is wrong with this pattern, especially at index 0 ... any idea what I am missing? Thank.

[EDIT] So part of the problem was that I used Locale.getDefault (), so probably trying to parse a date in English using dateFormat in Portuguese ... with the correct Locale, I still get ParseException, but for this times with an offset of 20, which means that something happens wrong when analyzing the time zone ("BRT", in my case) ...

+2
source share
3 answers

probably due to locale.

Try to change

Locale.getDefault()

to

Locale.ENGLISH

like this

        String date_ = "Fri May 16 23:59:59 BRT 2014";
    DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
    Calendar date = Calendar.getInstance(TimeZone.getTimeZone("America/Sao_Paulo"));
    dateFormat.setCalendar(date);
    try {
        date.setTime(dateFormat.parse(date_));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
+1
source

Perhaps this template works for you "EEE MMM d HH: mm: ss Z yyyy" if you do not look at its website http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat .html

Example:

String pattern = "EEE MMM dd HH:mm:ss z yyyy";
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
        Calendar d = Calendar.getInstance();
        try {
            d.setTime(dateFormat.parse(String.valueOf(d)));
        } catch (ParseException e1) {
            // TODO Auto-generated catch block


e1.printStackTrace();
    }
0
source

Are you sure? The code worked fine on my machine

public static void main(String[] args) throws ParseException {
        String date = "Fri May 16 23:59:59 BRT 2014";
        String pattern = "EEE MMM dd HH:mm:ss z yyyy";
        DateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Sao_Paulo"));
        cal.setTime(dateFormat.parse(date));


}
0
source

All Articles