Loading core/java/android/text/format/DateFormat.java +54 −81 Original line number Diff line number Diff line Loading @@ -177,11 +177,13 @@ public class DateFormat { * @hide */ public static boolean is24HourFormat(Context context, int userHandle) { String value = Settings.System.getStringForUser(context.getContentResolver(), final String value = Settings.System.getStringForUser(context.getContentResolver(), Settings.System.TIME_12_24, userHandle); if (value != null) { return value.equals("24"); } if (value == null) { Locale locale = context.getResources().getConfiguration().locale; final Locale locale = context.getResources().getConfiguration().locale; synchronized (sLocaleLock) { if (sIs24HourLocale != null && sIs24HourLocale.equals(locale)) { Loading @@ -189,31 +191,24 @@ public class DateFormat { } } java.text.DateFormat natural = final java.text.DateFormat natural = java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale); final boolean is24Hour; if (natural instanceof SimpleDateFormat) { SimpleDateFormat sdf = (SimpleDateFormat) natural; String pattern = sdf.toPattern(); if (pattern.indexOf('H') >= 0) { value = "24"; } else { value = "12"; } final SimpleDateFormat sdf = (SimpleDateFormat) natural; final String pattern = sdf.toPattern(); is24Hour = hasDesignator(pattern, 'H'); } else { value = "12"; is24Hour = false; } synchronized (sLocaleLock) { sIs24HourLocale = locale; sIs24Hour = value.equals("24"); sIs24Hour = is24Hour; } return sIs24Hour; } return value.equals("24"); return is24Hour; } /** Loading Loading @@ -249,17 +244,18 @@ public class DateFormat { /** * Returns a {@link java.text.DateFormat} object that can format the time according * to the current locale and the user's 12-/24-hour clock preference. * to the context's locale and the user's 12-/24-hour clock preference. * @param context the application context * @return the {@link java.text.DateFormat} object that properly formats the time. */ public static java.text.DateFormat getTimeFormat(Context context) { return new java.text.SimpleDateFormat(getTimeFormatString(context)); final Locale locale = context.getResources().getConfiguration().locale; return new java.text.SimpleDateFormat(getTimeFormatString(context), locale); } /** * Returns a String pattern that can be used to format the time according * to the current locale and the user's 12-/24-hour clock preference. * to the context's locale and the user's 12-/24-hour clock preference. * @param context the application context * @hide */ Loading @@ -269,45 +265,48 @@ public class DateFormat { /** * Returns a String pattern that can be used to format the time according * to the current locale and the user's 12-/24-hour clock preference. * to the context's locale and the user's 12-/24-hour clock preference. * @param context the application context * @param userHandle the user handle of the user to query the format for * @hide */ public static String getTimeFormatString(Context context, int userHandle) { LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); final LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); return is24HourFormat(context, userHandle) ? d.timeFormat_Hm : d.timeFormat_hm; } /** * Returns a {@link java.text.DateFormat} object that can format the date * in short form according to the current locale. * in short form according to the context's locale. * * @param context the application context * @return the {@link java.text.DateFormat} object that properly formats the date. */ public static java.text.DateFormat getDateFormat(Context context) { return java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT); final Locale locale = context.getResources().getConfiguration().locale; return java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, locale); } /** * Returns a {@link java.text.DateFormat} object that can format the date * in long form (such as {@code Monday, January 3, 2000}) for the current locale. * in long form (such as {@code Monday, January 3, 2000}) for the context's locale. * @param context the application context * @return the {@link java.text.DateFormat} object that formats the date in long form. */ public static java.text.DateFormat getLongDateFormat(Context context) { return java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG); final Locale locale = context.getResources().getConfiguration().locale; return java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG, locale); } /** * Returns a {@link java.text.DateFormat} object that can format the date * in medium form (such as {@code Jan 3, 2000}) for the current locale. * in medium form (such as {@code Jan 3, 2000}) for the context's locale. * @param context the application context * @return the {@link java.text.DateFormat} object that formats the date in long form. */ public static java.text.DateFormat getMediumDateFormat(Context context) { return java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM); final Locale locale = context.getResources().getConfiguration().locale; return java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM, locale); } /** Loading @@ -320,11 +319,13 @@ public class DateFormat { * order returned here. */ public static char[] getDateFormatOrder(Context context) { return ICU.getDateFormatOrder(getDateFormatString()); return ICU.getDateFormatOrder(getDateFormatString(context)); } private static String getDateFormatString() { java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT); private static String getDateFormatString(Context context) { final Locale locale = context.getResources().getConfiguration().locale; java.text.DateFormat df = java.text.DateFormat.getDateInstance( java.text.DateFormat.SHORT, locale); if (df instanceof SimpleDateFormat) { return ((SimpleDateFormat) df).toPattern(); } Loading Loading @@ -375,6 +376,9 @@ public class DateFormat { * Test if a format string contains the given designator. Always returns * {@code false} if the input format is {@code null}. * * Note that this is intended for searching for designators, not arbitrary * characters. So searching for a literal single quote would not work correctly. * * @hide */ public static boolean hasDesignator(CharSequence inFormat, char designator) { Loading @@ -382,50 +386,19 @@ public class DateFormat { final int length = inFormat.length(); int c; int count; for (int i = 0; i < length; i += count) { count = 1; c = inFormat.charAt(i); boolean insideQuote = false; for (int i = 0; i < length; i++) { final char c = inFormat.charAt(i); if (c == QUOTE) { count = skipQuotedText(inFormat, i, length); } else if (c == designator) { insideQuote = !insideQuote; } else if (!insideQuote) { if (c == designator) { return true; } } return false; } private static int skipQuotedText(CharSequence s, int i, int len) { if (i + 1 < len && s.charAt(i + 1) == QUOTE) { return 2; } int count = 1; // skip leading quote i++; while (i < len) { char c = s.charAt(i); if (c == QUOTE) { count++; // QUOTEQUOTE -> QUOTE if (i + 1 < len && s.charAt(i + 1) == QUOTE) { i++; } else { break; } } else { i++; count++; } } return count; return false; } /** Loading Loading
core/java/android/text/format/DateFormat.java +54 −81 Original line number Diff line number Diff line Loading @@ -177,11 +177,13 @@ public class DateFormat { * @hide */ public static boolean is24HourFormat(Context context, int userHandle) { String value = Settings.System.getStringForUser(context.getContentResolver(), final String value = Settings.System.getStringForUser(context.getContentResolver(), Settings.System.TIME_12_24, userHandle); if (value != null) { return value.equals("24"); } if (value == null) { Locale locale = context.getResources().getConfiguration().locale; final Locale locale = context.getResources().getConfiguration().locale; synchronized (sLocaleLock) { if (sIs24HourLocale != null && sIs24HourLocale.equals(locale)) { Loading @@ -189,31 +191,24 @@ public class DateFormat { } } java.text.DateFormat natural = final java.text.DateFormat natural = java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale); final boolean is24Hour; if (natural instanceof SimpleDateFormat) { SimpleDateFormat sdf = (SimpleDateFormat) natural; String pattern = sdf.toPattern(); if (pattern.indexOf('H') >= 0) { value = "24"; } else { value = "12"; } final SimpleDateFormat sdf = (SimpleDateFormat) natural; final String pattern = sdf.toPattern(); is24Hour = hasDesignator(pattern, 'H'); } else { value = "12"; is24Hour = false; } synchronized (sLocaleLock) { sIs24HourLocale = locale; sIs24Hour = value.equals("24"); sIs24Hour = is24Hour; } return sIs24Hour; } return value.equals("24"); return is24Hour; } /** Loading Loading @@ -249,17 +244,18 @@ public class DateFormat { /** * Returns a {@link java.text.DateFormat} object that can format the time according * to the current locale and the user's 12-/24-hour clock preference. * to the context's locale and the user's 12-/24-hour clock preference. * @param context the application context * @return the {@link java.text.DateFormat} object that properly formats the time. */ public static java.text.DateFormat getTimeFormat(Context context) { return new java.text.SimpleDateFormat(getTimeFormatString(context)); final Locale locale = context.getResources().getConfiguration().locale; return new java.text.SimpleDateFormat(getTimeFormatString(context), locale); } /** * Returns a String pattern that can be used to format the time according * to the current locale and the user's 12-/24-hour clock preference. * to the context's locale and the user's 12-/24-hour clock preference. * @param context the application context * @hide */ Loading @@ -269,45 +265,48 @@ public class DateFormat { /** * Returns a String pattern that can be used to format the time according * to the current locale and the user's 12-/24-hour clock preference. * to the context's locale and the user's 12-/24-hour clock preference. * @param context the application context * @param userHandle the user handle of the user to query the format for * @hide */ public static String getTimeFormatString(Context context, int userHandle) { LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); final LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); return is24HourFormat(context, userHandle) ? d.timeFormat_Hm : d.timeFormat_hm; } /** * Returns a {@link java.text.DateFormat} object that can format the date * in short form according to the current locale. * in short form according to the context's locale. * * @param context the application context * @return the {@link java.text.DateFormat} object that properly formats the date. */ public static java.text.DateFormat getDateFormat(Context context) { return java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT); final Locale locale = context.getResources().getConfiguration().locale; return java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, locale); } /** * Returns a {@link java.text.DateFormat} object that can format the date * in long form (such as {@code Monday, January 3, 2000}) for the current locale. * in long form (such as {@code Monday, January 3, 2000}) for the context's locale. * @param context the application context * @return the {@link java.text.DateFormat} object that formats the date in long form. */ public static java.text.DateFormat getLongDateFormat(Context context) { return java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG); final Locale locale = context.getResources().getConfiguration().locale; return java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG, locale); } /** * Returns a {@link java.text.DateFormat} object that can format the date * in medium form (such as {@code Jan 3, 2000}) for the current locale. * in medium form (such as {@code Jan 3, 2000}) for the context's locale. * @param context the application context * @return the {@link java.text.DateFormat} object that formats the date in long form. */ public static java.text.DateFormat getMediumDateFormat(Context context) { return java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM); final Locale locale = context.getResources().getConfiguration().locale; return java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM, locale); } /** Loading @@ -320,11 +319,13 @@ public class DateFormat { * order returned here. */ public static char[] getDateFormatOrder(Context context) { return ICU.getDateFormatOrder(getDateFormatString()); return ICU.getDateFormatOrder(getDateFormatString(context)); } private static String getDateFormatString() { java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT); private static String getDateFormatString(Context context) { final Locale locale = context.getResources().getConfiguration().locale; java.text.DateFormat df = java.text.DateFormat.getDateInstance( java.text.DateFormat.SHORT, locale); if (df instanceof SimpleDateFormat) { return ((SimpleDateFormat) df).toPattern(); } Loading Loading @@ -375,6 +376,9 @@ public class DateFormat { * Test if a format string contains the given designator. Always returns * {@code false} if the input format is {@code null}. * * Note that this is intended for searching for designators, not arbitrary * characters. So searching for a literal single quote would not work correctly. * * @hide */ public static boolean hasDesignator(CharSequence inFormat, char designator) { Loading @@ -382,50 +386,19 @@ public class DateFormat { final int length = inFormat.length(); int c; int count; for (int i = 0; i < length; i += count) { count = 1; c = inFormat.charAt(i); boolean insideQuote = false; for (int i = 0; i < length; i++) { final char c = inFormat.charAt(i); if (c == QUOTE) { count = skipQuotedText(inFormat, i, length); } else if (c == designator) { insideQuote = !insideQuote; } else if (!insideQuote) { if (c == designator) { return true; } } return false; } private static int skipQuotedText(CharSequence s, int i, int len) { if (i + 1 < len && s.charAt(i + 1) == QUOTE) { return 2; } int count = 1; // skip leading quote i++; while (i < len) { char c = s.charAt(i); if (c == QUOTE) { count++; // QUOTEQUOTE -> QUOTE if (i + 1 < len && s.charAt(i + 1) == QUOTE) { i++; } else { break; } } else { i++; count++; } } return count; return false; } /** Loading