AND4011002849 AND4011002849 - 1 month ago 16
Java Question

Format parse exception “EEE MMM dd HH:mm:ss Z yyyy”

My date string is: "Wed Oct 19 14:34:26 BRST 2016" and I'm trying to parse it to "dd/MM/yyyy", but I'm getting the following exception:

java.text.ParseException: Unparseable date: "Wed Oct 19 14:34:26 BRST 2016" (at offset 20)


the method

public String dataText(int lastintervalo) {

Date mDate = new Date();
String dt = mDate.toString();
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.getDefault());
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(dt));

} catch (ParseException e) {
e.printStackTrace();
}
sdf.applyPattern("dd/MM/yyyy");

c.add(Calendar.DATE, lastintervalo);
return sdf.format(c.getTime());

}


I already searched on google and stackoverflow questions, but nothing seems to work

Answer

Since the error message is complaining about offset 20, which is the BRST value, it seems that it cannot resolve the time zone.

Please try this code, that should ensure that the Brazilian time zone is recognized:

SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
System.out.println(sdf.parse("Wed Oct 19 14:34:26 BRST 2016"));

Since I'm in Eastern US, that prints this for me:

Wed Oct 19 12:34:26 EDT 2016
Comments