Loading core/java/android/text/format/DateFormat.java +11 −2 Original line number Diff line number Diff line Loading @@ -170,9 +170,18 @@ public class DateFormat { * @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)); } /** * 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. * @param context the application context * @hide */ public static String getTimeFormatString(Context context) { LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); boolean is24 = is24HourFormat(context); return new java.text.SimpleDateFormat(is24 ? d.timeFormat24 : d.timeFormat12); return is24HourFormat(context) ? d.timeFormat24 : d.timeFormat12; } /** Loading core/java/android/widget/DigitalClock.java +1 −14 Original line number Diff line number Diff line Loading @@ -39,8 +39,6 @@ public class DigitalClock extends TextView { // proportional fonts don't shake rendering Calendar mCalendar; private final static String m12 = "h:mm:ss aa"; private final static String m24 = "k:mm:ss"; @SuppressWarnings("FieldCanBeLocal") // We must keep a reference to this observer private FormatChangeObserver mFormatChangeObserver; Loading Loading @@ -102,19 +100,8 @@ public class DigitalClock extends TextView { mTickerStopped = true; } /** * Pulls 12/24 mode from system settings */ private boolean get24HourMode() { return android.text.format.DateFormat.is24HourFormat(getContext()); } private void setFormat() { if (get24HourMode()) { mFormat = m24; } else { mFormat = m12; } mFormat = DateFormat.getTimeFormatString(getContext()); } private class FormatChangeObserver extends ContentObserver { Loading core/java/android/widget/TextClock.java +62 −49 Original line number Diff line number Diff line Loading @@ -36,6 +36,8 @@ import com.android.internal.R; import java.util.Calendar; import java.util.TimeZone; import libcore.icu.LocaleData; import static android.view.ViewDebug.ExportedProperty; import static android.widget.RemoteViews.*; Loading @@ -46,7 +48,8 @@ import static android.widget.RemoteViews.*; * <p>This view honors the 24-hour format system setting. As such, it is * possible and recommended to provide two different formatting patterns: * one to display the date/time in 24-hour mode and one to display the * date/time in 12-hour mode.</p> * date/time in 12-hour mode. Most callers will want to use the defaults, * though, which will be appropriate for the user's locale.</p> * * <p>It is possible to determine whether the system is currently in * 24-hour mode by calling {@link #is24HourModeEnabled()}.</p> Loading @@ -58,21 +61,23 @@ import static android.widget.RemoteViews.*; * <ul> * <li>Use the value returned by {@link #getFormat24Hour()} when non-null</li> * <li>Otherwise, use the value returned by {@link #getFormat12Hour()} when non-null</li> * <li>Otherwise, use {@link #DEFAULT_FORMAT_24_HOUR}</li> * <li>Otherwise, use a default value appropriate for the user's locale, such as {@code h:mm a}</li> * </ul> * </li> * <li>In 12-hour mode: * <ul> * <li>Use the value returned by {@link #getFormat12Hour()} when non-null</li> * <li>Otherwise, use the value returned by {@link #getFormat24Hour()} when non-null</li> * <li>Otherwise, use {@link #DEFAULT_FORMAT_12_HOUR}</li> * <li>Otherwise, use a default value appropriate for the user's locale, such as {@code HH:mm}</li> * </ul> * </li> * </ul> * * <p>The {@link CharSequence} instances used as formatting patterns when calling either * {@link #setFormat24Hour(CharSequence)} or {@link #setFormat12Hour(CharSequence)} can * contain styling information. To do so, use a {@link android.text.Spanned} object.</p> * contain styling information. To do so, use a {@link android.text.Spanned} object. * Note that if you customize these strings, it is your responsibility to supply strings * appropriate for formatting dates and/or times in the user's locale.</p> * * @attr ref android.R.styleable#TextClock_format12Hour * @attr ref android.R.styleable#TextClock_format24Hour Loading @@ -81,7 +86,7 @@ import static android.widget.RemoteViews.*; @RemoteView public class TextClock extends TextView { /** * The default formatting pattern in 12-hour mode. This pattenr is used * The default formatting pattern in 12-hour mode. This pattern is used * if {@link #setFormat12Hour(CharSequence)} is called with a null pattern * or if no pattern was specified when creating an instance of this class. * Loading @@ -90,11 +95,12 @@ public class TextClock extends TextView { * * @see #setFormat12Hour(CharSequence) * @see #getFormat12Hour() * @deprecated Let the system use locale-appropriate defaults instead. */ public static final CharSequence DEFAULT_FORMAT_12_HOUR = "h:mm aa"; public static final CharSequence DEFAULT_FORMAT_12_HOUR = "h:mm a"; /** * The default formatting pattern in 24-hour mode. This pattenr is used * The default formatting pattern in 24-hour mode. This pattern is used * if {@link #setFormat24Hour(CharSequence)} is called with a null pattern * or if no pattern was specified when creating an instance of this class. * Loading @@ -102,11 +108,12 @@ public class TextClock extends TextView { * * @see #setFormat24Hour(CharSequence) * @see #getFormat24Hour() * @deprecated Let the system use locale-appropriate defaults instead. */ public static final CharSequence DEFAULT_FORMAT_24_HOUR = "k:mm"; public static final CharSequence DEFAULT_FORMAT_24_HOUR = "H:mm"; private CharSequence mFormat12 = DEFAULT_FORMAT_12_HOUR; private CharSequence mFormat24 = DEFAULT_FORMAT_24_HOUR; private CharSequence mFormat12; private CharSequence mFormat24; @ExportedProperty private CharSequence mFormat; Loading Loading @@ -201,14 +208,8 @@ public class TextClock extends TextView { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextClock, defStyle, 0); try { CharSequence format; format = a.getText(R.styleable.TextClock_format12Hour); mFormat12 = format == null ? DEFAULT_FORMAT_12_HOUR : format; format = a.getText(R.styleable.TextClock_format24Hour); mFormat24 = format == null ? DEFAULT_FORMAT_24_HOUR : format; mFormat12 = a.getText(R.styleable.TextClock_format12Hour); mFormat24 = a.getText(R.styleable.TextClock_format24Hour); mTimeZone = a.getString(R.styleable.TextClock_timeZone); } finally { a.recycle(); Loading @@ -218,6 +219,16 @@ public class TextClock extends TextView { } private void init() { if (mFormat12 == null || mFormat24 == null) { LocaleData ld = LocaleData.get(getContext().getResources().getConfiguration().locale); if (mFormat12 == null) { mFormat12 = ld.timeFormat12; } if (mFormat24 == null) { mFormat24 = ld.timeFormat24; } } createTime(mTimeZone); // Wait until onAttachedToWindow() to handle the ticker chooseFormat(false); Loading Loading @@ -395,10 +406,12 @@ public class TextClock extends TextView { private void chooseFormat(boolean handleTicker) { final boolean format24Requested = is24HourModeEnabled(); LocaleData ld = LocaleData.get(getContext().getResources().getConfiguration().locale); if (format24Requested) { mFormat = abc(mFormat24, mFormat12, DEFAULT_FORMAT_24_HOUR); mFormat = abc(mFormat24, mFormat12, ld.timeFormat24); } else { mFormat = abc(mFormat12, mFormat24, DEFAULT_FORMAT_12_HOUR); mFormat = abc(mFormat12, mFormat24, ld.timeFormat12); } boolean hadSeconds = mHasSeconds; Loading core/res/res/values/attrs.xml +2 −2 Original line number Diff line number Diff line Loading @@ -3050,12 +3050,12 @@ <!-- Specifies the formatting pattern used to show the time and/or date in 12-hour mode. Please refer to {@link android.text.format.DateFormat} for a complete description of accepted formatting patterns. The default pattern is "h:mm aa". --> The default pattern is a locale-appropriate equivalent of "h:mm a". --> <attr name="format12Hour" format="string"/> <!-- Specifies the formatting pattern used to show the time and/or date in 24-hour mode. Please refer to {@link android.text.format.DateFormat} for a complete description of accepted formatting patterns. The default pattern is "k:mm". --> The default pattern is a locale-appropriate equivalent of "H:mm". --> <attr name="format24Hour" format="string"/> <!-- Specifies the time zone to use. When this attribute is specified, the TextClock will ignore the time zone of the system. To use the user's Loading Loading
core/java/android/text/format/DateFormat.java +11 −2 Original line number Diff line number Diff line Loading @@ -170,9 +170,18 @@ public class DateFormat { * @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)); } /** * 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. * @param context the application context * @hide */ public static String getTimeFormatString(Context context) { LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); boolean is24 = is24HourFormat(context); return new java.text.SimpleDateFormat(is24 ? d.timeFormat24 : d.timeFormat12); return is24HourFormat(context) ? d.timeFormat24 : d.timeFormat12; } /** Loading
core/java/android/widget/DigitalClock.java +1 −14 Original line number Diff line number Diff line Loading @@ -39,8 +39,6 @@ public class DigitalClock extends TextView { // proportional fonts don't shake rendering Calendar mCalendar; private final static String m12 = "h:mm:ss aa"; private final static String m24 = "k:mm:ss"; @SuppressWarnings("FieldCanBeLocal") // We must keep a reference to this observer private FormatChangeObserver mFormatChangeObserver; Loading Loading @@ -102,19 +100,8 @@ public class DigitalClock extends TextView { mTickerStopped = true; } /** * Pulls 12/24 mode from system settings */ private boolean get24HourMode() { return android.text.format.DateFormat.is24HourFormat(getContext()); } private void setFormat() { if (get24HourMode()) { mFormat = m24; } else { mFormat = m12; } mFormat = DateFormat.getTimeFormatString(getContext()); } private class FormatChangeObserver extends ContentObserver { Loading
core/java/android/widget/TextClock.java +62 −49 Original line number Diff line number Diff line Loading @@ -36,6 +36,8 @@ import com.android.internal.R; import java.util.Calendar; import java.util.TimeZone; import libcore.icu.LocaleData; import static android.view.ViewDebug.ExportedProperty; import static android.widget.RemoteViews.*; Loading @@ -46,7 +48,8 @@ import static android.widget.RemoteViews.*; * <p>This view honors the 24-hour format system setting. As such, it is * possible and recommended to provide two different formatting patterns: * one to display the date/time in 24-hour mode and one to display the * date/time in 12-hour mode.</p> * date/time in 12-hour mode. Most callers will want to use the defaults, * though, which will be appropriate for the user's locale.</p> * * <p>It is possible to determine whether the system is currently in * 24-hour mode by calling {@link #is24HourModeEnabled()}.</p> Loading @@ -58,21 +61,23 @@ import static android.widget.RemoteViews.*; * <ul> * <li>Use the value returned by {@link #getFormat24Hour()} when non-null</li> * <li>Otherwise, use the value returned by {@link #getFormat12Hour()} when non-null</li> * <li>Otherwise, use {@link #DEFAULT_FORMAT_24_HOUR}</li> * <li>Otherwise, use a default value appropriate for the user's locale, such as {@code h:mm a}</li> * </ul> * </li> * <li>In 12-hour mode: * <ul> * <li>Use the value returned by {@link #getFormat12Hour()} when non-null</li> * <li>Otherwise, use the value returned by {@link #getFormat24Hour()} when non-null</li> * <li>Otherwise, use {@link #DEFAULT_FORMAT_12_HOUR}</li> * <li>Otherwise, use a default value appropriate for the user's locale, such as {@code HH:mm}</li> * </ul> * </li> * </ul> * * <p>The {@link CharSequence} instances used as formatting patterns when calling either * {@link #setFormat24Hour(CharSequence)} or {@link #setFormat12Hour(CharSequence)} can * contain styling information. To do so, use a {@link android.text.Spanned} object.</p> * contain styling information. To do so, use a {@link android.text.Spanned} object. * Note that if you customize these strings, it is your responsibility to supply strings * appropriate for formatting dates and/or times in the user's locale.</p> * * @attr ref android.R.styleable#TextClock_format12Hour * @attr ref android.R.styleable#TextClock_format24Hour Loading @@ -81,7 +86,7 @@ import static android.widget.RemoteViews.*; @RemoteView public class TextClock extends TextView { /** * The default formatting pattern in 12-hour mode. This pattenr is used * The default formatting pattern in 12-hour mode. This pattern is used * if {@link #setFormat12Hour(CharSequence)} is called with a null pattern * or if no pattern was specified when creating an instance of this class. * Loading @@ -90,11 +95,12 @@ public class TextClock extends TextView { * * @see #setFormat12Hour(CharSequence) * @see #getFormat12Hour() * @deprecated Let the system use locale-appropriate defaults instead. */ public static final CharSequence DEFAULT_FORMAT_12_HOUR = "h:mm aa"; public static final CharSequence DEFAULT_FORMAT_12_HOUR = "h:mm a"; /** * The default formatting pattern in 24-hour mode. This pattenr is used * The default formatting pattern in 24-hour mode. This pattern is used * if {@link #setFormat24Hour(CharSequence)} is called with a null pattern * or if no pattern was specified when creating an instance of this class. * Loading @@ -102,11 +108,12 @@ public class TextClock extends TextView { * * @see #setFormat24Hour(CharSequence) * @see #getFormat24Hour() * @deprecated Let the system use locale-appropriate defaults instead. */ public static final CharSequence DEFAULT_FORMAT_24_HOUR = "k:mm"; public static final CharSequence DEFAULT_FORMAT_24_HOUR = "H:mm"; private CharSequence mFormat12 = DEFAULT_FORMAT_12_HOUR; private CharSequence mFormat24 = DEFAULT_FORMAT_24_HOUR; private CharSequence mFormat12; private CharSequence mFormat24; @ExportedProperty private CharSequence mFormat; Loading Loading @@ -201,14 +208,8 @@ public class TextClock extends TextView { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextClock, defStyle, 0); try { CharSequence format; format = a.getText(R.styleable.TextClock_format12Hour); mFormat12 = format == null ? DEFAULT_FORMAT_12_HOUR : format; format = a.getText(R.styleable.TextClock_format24Hour); mFormat24 = format == null ? DEFAULT_FORMAT_24_HOUR : format; mFormat12 = a.getText(R.styleable.TextClock_format12Hour); mFormat24 = a.getText(R.styleable.TextClock_format24Hour); mTimeZone = a.getString(R.styleable.TextClock_timeZone); } finally { a.recycle(); Loading @@ -218,6 +219,16 @@ public class TextClock extends TextView { } private void init() { if (mFormat12 == null || mFormat24 == null) { LocaleData ld = LocaleData.get(getContext().getResources().getConfiguration().locale); if (mFormat12 == null) { mFormat12 = ld.timeFormat12; } if (mFormat24 == null) { mFormat24 = ld.timeFormat24; } } createTime(mTimeZone); // Wait until onAttachedToWindow() to handle the ticker chooseFormat(false); Loading Loading @@ -395,10 +406,12 @@ public class TextClock extends TextView { private void chooseFormat(boolean handleTicker) { final boolean format24Requested = is24HourModeEnabled(); LocaleData ld = LocaleData.get(getContext().getResources().getConfiguration().locale); if (format24Requested) { mFormat = abc(mFormat24, mFormat12, DEFAULT_FORMAT_24_HOUR); mFormat = abc(mFormat24, mFormat12, ld.timeFormat24); } else { mFormat = abc(mFormat12, mFormat24, DEFAULT_FORMAT_12_HOUR); mFormat = abc(mFormat12, mFormat24, ld.timeFormat12); } boolean hadSeconds = mHasSeconds; Loading
core/res/res/values/attrs.xml +2 −2 Original line number Diff line number Diff line Loading @@ -3050,12 +3050,12 @@ <!-- Specifies the formatting pattern used to show the time and/or date in 12-hour mode. Please refer to {@link android.text.format.DateFormat} for a complete description of accepted formatting patterns. The default pattern is "h:mm aa". --> The default pattern is a locale-appropriate equivalent of "h:mm a". --> <attr name="format12Hour" format="string"/> <!-- Specifies the formatting pattern used to show the time and/or date in 24-hour mode. Please refer to {@link android.text.format.DateFormat} for a complete description of accepted formatting patterns. The default pattern is "k:mm". --> The default pattern is a locale-appropriate equivalent of "H:mm". --> <attr name="format24Hour" format="string"/> <!-- Specifies the time zone to use. When this attribute is specified, the TextClock will ignore the time zone of the system. To use the user's Loading