From f9818d33f94686ee9c80697ec79ef557a7b77176 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 16 Aug 2013 11:56:35 -0700 Subject: [PATCH] Fix NPE in DateFormat.is24HourFormat. In some cases, we end up being called by code that doesn't have a valid Context. It got away with this historically because it wasn't formatting times (just dates), so it never went down a path that tried to query the user's 12/24-hour preference. This patch just ensures that we don't try to get the preference unless we actually need it. Bug: 10339015 (cherry picked from commit 8d8ef00c8276200f108433922761401817fd9a50) Change-Id: If074a67fa52943c0ec7bb5c5bbe5a11f54fb1f97 --- core/java/android/text/format/DateUtils.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/java/android/text/format/DateUtils.java b/core/java/android/text/format/DateUtils.java index 2b805a9bc367..22675b4af479 100644 --- a/core/java/android/text/format/DateUtils.java +++ b/core/java/android/text/format/DateUtils.java @@ -816,9 +816,10 @@ public class DateUtils */ public static Formatter formatDateRange(Context context, Formatter formatter, long startMillis, long endMillis, int flags, String timeZone) { - // icu4c will fall back to the locale's preferred 12/24 format, + // If we're being asked to format a time without being explicitly told whether to use + // the 12- or 24-hour clock, icu4c will fall back to the locale's preferred 12/24 format, // but we want to fall back to the user's preference. - if ((flags & (FORMAT_12HOUR | FORMAT_24HOUR)) == 0) { + if ((flags & (FORMAT_SHOW_TIME | FORMAT_12HOUR | FORMAT_24HOUR)) == FORMAT_SHOW_TIME) { flags |= DateFormat.is24HourFormat(context) ? FORMAT_24HOUR : FORMAT_12HOUR; } -- 2.11.0