Written by Nick Holcombe
Dec 09, 2015

Android Marshmallow and SimpleDateFormat

Android Marshmallow and a breaking change with SimpleDateFormat

Changing the clocks

In Android 6.0 (Marshmallow) they've changed the clocks. We did a little exploration to find out why and figure out a solution.

Date Formatting Scenario

Our app is expecting a date from the server in the format Nov 26, 2015 8:49:51 AM and we were parsing it using the following simplified code

String strDate = “Nov 26, 2015 8:49:51 AM”;

try {
  Date date = new SimpleDateFormat("MMM d, yyyy h:mm:ss a", Locale.UK).parse(strDate);
  Log.d("DateParser", "Date: "+date);
} catch (ParseException e) {

In Android versions 5.1.1 and below this code was working fine, but with the new Android 6.0, Marshmallow, the code throws the Exception “java.text.ParseException: Unparseable date”

So, why exactly? 

The issue

It turns out that as part of the Marshmallow release, the Locale.UK definitions used by the SimpleDateFormat were updated.  

Specifically the AM/PM segment that is recognised by the date format pattern “a” was changed from am to a.m. and pm to p.m. - meaning that in Marshmallow, our code was now expecting a date format of Nov 26, 2015 8:49:51 A.M. resulting in the ParseException.

The solution

The issue was fixed by using a Locale.US instead. Which admittedly, we should have been using anyway for machine readable dates, but we wouldn’t have expected our code to break just by upgrading the Android version.  

I wonder how many other apps on the Play Store are going to start breaking when they run under Marshmallow?

Was the breaking change a mistake?

This stackoverflow article cites the change as coming “as part of the Locale updates in March 2015, the en_gb locale had its am/pm definitions replaced. source diff ”.

If you follow the source diff link it shows the changes coming from the International Components for Unicode library (ICU), release candidate 55 (March 2015).  

The ICU is a “widely used set of C/C++ and Java libraries providing Unicode and Globalization support for software applications.”, so presumably other systems and projects (e.g. Apple iOS and Apache) could break too if they used this updated definition with a UK Locale.

Interestingly, I noticed that release 56 (September 2015) reverts back to the am and pm definitions - so presumably our code would have started working again if we could have waited for a future release of Android to include this new set of definitions...

...which makes me wonder...

Presumably a future version of Android will, at some point, revert back to the original definitions again, so will that cause other Android apps to start breaking if developers in the meantime start adopting the (temporary) a.m. format in their code!

Watch this space.