I have been developing my app and testing with emulators and devices with Api 22 and 23
but when I tested on a device with API 25
or Version 7.1 Nougat
today I am suddenly experiencing the following Exception:
W/System.err: java.text.ParseException: Unparseable date: "2017-02-08 18:39:21+00"
W/System.err: at java.text.DateFormat.parse(DateFormat.java:358)
I am parsing the date "2017-02-08 18:39:21+00"
using the following pattern "yyyy-MM-dd HH:mm:ssZ"
and this has been working fine until I tested in an emulator running API 25
and I also tried it on a Nexus 6P
running the same version and I get the same exception.
The following method is where the exception occurs on API 25
. It works fine on the other versions I am testing on.
private static final String SERVER_DATE_FORMAT = "yyyy-MM-dd HH:mm:ssZ";
public static Date javaDateFromStringDate(String stringDate) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(SERVER_DATE_FORMAT, Locale.getDefault());
return format.parse(stringDate);
}
The answers was indeed to use X
for two-digit timezones per the documentation. Thanks to @commonsware and @blizzard in the comments. It appears Android didn't seem to mind if you used Z
for two-digit timezones but got stricter about it in Nougat
.