Loading core/java/android/text/format/DateFormat.java +16 −18 Original line number Diff line number Diff line Loading @@ -284,6 +284,12 @@ public class DateFormat { */ public static java.text.DateFormat getDateFormatForSetting(Context context, String value) { String format = getDateFormatStringForSetting(context, value); return new java.text.SimpleDateFormat(format); } private static String getDateFormatStringForSetting(Context context, String value) { if (value != null) { int month = value.indexOf('M'); int day = value.indexOf('d'); Loading @@ -291,7 +297,7 @@ public class DateFormat { if (month >= 0 && day >= 0 && year >= 0) { String template = context.getString(R.string.numeric_date_template); if (year < month) { if (year < month && year < day) { if (month < day) { value = String.format(template, "yyyy", "MM", "dd"); } else { Loading @@ -311,7 +317,7 @@ public class DateFormat { } } return new java.text.SimpleDateFormat(value); return value; } } Loading @@ -321,7 +327,7 @@ public class DateFormat { * so that we get a four-digit year instead a two-digit year. */ value = context.getString(R.string.numeric_date_format); return new java.text.SimpleDateFormat(value); return value; } /** Loading @@ -347,7 +353,11 @@ public class DateFormat { /** * Gets the current date format stored as a char array. The array will contain * 3 elements ({@link #DATE}, {@link #MONTH}, and {@link #YEAR}) in the order * preferred by the user. * specified by the user's format preference. Note that this order is * only appropriate for all-numeric dates; spelled-out (MEDIUM and LONG) * dates will generally contain other punctuation, spaces, or words, * not just the day, month, and year, and not necessarily in the same * order returned here. */ public static final char[] getDateFormatOrder(Context context) { char[] order = new char[] {DATE, MONTH, YEAR}; Loading Loading @@ -380,22 +390,10 @@ public class DateFormat { } private static String getDateFormatString(Context context) { java.text.DateFormat df; df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT); if (df instanceof SimpleDateFormat) { return ((SimpleDateFormat) df).toPattern(); } String value = Settings.System.getString(context.getContentResolver(), Settings.System.DATE_FORMAT); if (value == null || value.length() < 6) { /* * No need to localize -- this is an emergency fallback in case * the setting is missing, but it should always be set. */ value = "MM-dd-yyyy"; } return value; return getDateFormatStringForSetting(context, value); } /** Loading core/java/android/widget/DatePicker.java +60 −17 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ import com.android.internal.widget.NumberPicker; import com.android.internal.widget.NumberPicker.OnChangedListener; import java.text.DateFormatSymbols; import java.text.SimpleDateFormat; import java.util.Calendar; /** Loading Loading @@ -101,7 +102,8 @@ public class DatePicker extends FrameLayout { mMonthPicker = (NumberPicker) findViewById(R.id.month); mMonthPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER); DateFormatSymbols dfs = new DateFormatSymbols(); mMonthPicker.setRange(1, 12, dfs.getShortMonths()); String[] months = dfs.getShortMonths(); mMonthPicker.setRange(1, 12, months); mMonthPicker.setSpeed(200); mMonthPicker.setOnChangeListener(new OnChangedListener() { public void onChanged(NumberPicker picker, int oldVal, int newVal) { Loading Loading @@ -146,7 +148,7 @@ public class DatePicker extends FrameLayout { init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null); // re-order the number pickers to match the current date format reorderPickers(); reorderPickers(months); if (!isEnabled()) { setEnabled(false); Loading @@ -161,14 +163,28 @@ public class DatePicker extends FrameLayout { mYearPicker.setEnabled(enabled); } private void reorderPickers() { char[] order = DateFormat.getDateFormatOrder(mContext); private void reorderPickers(String[] months) { java.text.DateFormat format; String order; /* Default order is month, date, year so if that's the order then * do nothing. /* * If the user is in a locale where the medium date format is * still numeric (Japanese and Czech, for example), respect * the date format order setting. Otherwise, use the order * that the locale says is appropriate for a spelled-out date. */ if ((order[0] == DateFormat.MONTH) && (order[1] == DateFormat.DATE)) { return; if (months[0].startsWith("1")) { format = DateFormat.getDateFormat(getContext()); } else { format = DateFormat.getMediumDateFormat(getContext()); } if (format instanceof SimpleDateFormat) { order = ((SimpleDateFormat) format).toPattern(); } else { // Shouldn't happen, but just in case. order = new String(DateFormat.getDateFormatOrder(getContext())); } /* Remove the 3 pickers from their parent and then add them back in the Loading @@ -176,14 +192,40 @@ public class DatePicker extends FrameLayout { */ LinearLayout parent = (LinearLayout) findViewById(R.id.parent); parent.removeAllViews(); for (char c : order) { if (c == DateFormat.DATE) { boolean quoted = false; boolean didDay = false, didMonth = false, didYear = false; for (int i = 0; i < order.length(); i++) { char c = order.charAt(i); if (c == '\'') { quoted = !quoted; } if (!quoted) { if (c == DateFormat.DATE && !didDay) { parent.addView(mDayPicker); } else if (c == DateFormat.MONTH) { didDay = true; } else if ((c == DateFormat.MONTH || c == 'L') && !didMonth) { parent.addView(mMonthPicker); } else { didMonth = true; } else if (c == DateFormat.YEAR && !didYear) { parent.addView (mYearPicker); didYear = true; } } } // Shouldn't happen, but just in case. if (!didMonth) { parent.addView(mMonthPicker); } if (!didDay) { parent.addView(mDayPicker); } if (!didYear) { parent.addView(mYearPicker); } } Loading @@ -192,6 +234,7 @@ public class DatePicker extends FrameLayout { mMonth = monthOfYear; mDay = dayOfMonth; updateSpinners(); reorderPickers(new DateFormatSymbols().getShortMonths()); } private static class SavedState extends BaseSavedState { Loading Loading
core/java/android/text/format/DateFormat.java +16 −18 Original line number Diff line number Diff line Loading @@ -284,6 +284,12 @@ public class DateFormat { */ public static java.text.DateFormat getDateFormatForSetting(Context context, String value) { String format = getDateFormatStringForSetting(context, value); return new java.text.SimpleDateFormat(format); } private static String getDateFormatStringForSetting(Context context, String value) { if (value != null) { int month = value.indexOf('M'); int day = value.indexOf('d'); Loading @@ -291,7 +297,7 @@ public class DateFormat { if (month >= 0 && day >= 0 && year >= 0) { String template = context.getString(R.string.numeric_date_template); if (year < month) { if (year < month && year < day) { if (month < day) { value = String.format(template, "yyyy", "MM", "dd"); } else { Loading @@ -311,7 +317,7 @@ public class DateFormat { } } return new java.text.SimpleDateFormat(value); return value; } } Loading @@ -321,7 +327,7 @@ public class DateFormat { * so that we get a four-digit year instead a two-digit year. */ value = context.getString(R.string.numeric_date_format); return new java.text.SimpleDateFormat(value); return value; } /** Loading @@ -347,7 +353,11 @@ public class DateFormat { /** * Gets the current date format stored as a char array. The array will contain * 3 elements ({@link #DATE}, {@link #MONTH}, and {@link #YEAR}) in the order * preferred by the user. * specified by the user's format preference. Note that this order is * only appropriate for all-numeric dates; spelled-out (MEDIUM and LONG) * dates will generally contain other punctuation, spaces, or words, * not just the day, month, and year, and not necessarily in the same * order returned here. */ public static final char[] getDateFormatOrder(Context context) { char[] order = new char[] {DATE, MONTH, YEAR}; Loading Loading @@ -380,22 +390,10 @@ public class DateFormat { } private static String getDateFormatString(Context context) { java.text.DateFormat df; df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT); if (df instanceof SimpleDateFormat) { return ((SimpleDateFormat) df).toPattern(); } String value = Settings.System.getString(context.getContentResolver(), Settings.System.DATE_FORMAT); if (value == null || value.length() < 6) { /* * No need to localize -- this is an emergency fallback in case * the setting is missing, but it should always be set. */ value = "MM-dd-yyyy"; } return value; return getDateFormatStringForSetting(context, value); } /** Loading
core/java/android/widget/DatePicker.java +60 −17 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ import com.android.internal.widget.NumberPicker; import com.android.internal.widget.NumberPicker.OnChangedListener; import java.text.DateFormatSymbols; import java.text.SimpleDateFormat; import java.util.Calendar; /** Loading Loading @@ -101,7 +102,8 @@ public class DatePicker extends FrameLayout { mMonthPicker = (NumberPicker) findViewById(R.id.month); mMonthPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER); DateFormatSymbols dfs = new DateFormatSymbols(); mMonthPicker.setRange(1, 12, dfs.getShortMonths()); String[] months = dfs.getShortMonths(); mMonthPicker.setRange(1, 12, months); mMonthPicker.setSpeed(200); mMonthPicker.setOnChangeListener(new OnChangedListener() { public void onChanged(NumberPicker picker, int oldVal, int newVal) { Loading Loading @@ -146,7 +148,7 @@ public class DatePicker extends FrameLayout { init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null); // re-order the number pickers to match the current date format reorderPickers(); reorderPickers(months); if (!isEnabled()) { setEnabled(false); Loading @@ -161,14 +163,28 @@ public class DatePicker extends FrameLayout { mYearPicker.setEnabled(enabled); } private void reorderPickers() { char[] order = DateFormat.getDateFormatOrder(mContext); private void reorderPickers(String[] months) { java.text.DateFormat format; String order; /* Default order is month, date, year so if that's the order then * do nothing. /* * If the user is in a locale where the medium date format is * still numeric (Japanese and Czech, for example), respect * the date format order setting. Otherwise, use the order * that the locale says is appropriate for a spelled-out date. */ if ((order[0] == DateFormat.MONTH) && (order[1] == DateFormat.DATE)) { return; if (months[0].startsWith("1")) { format = DateFormat.getDateFormat(getContext()); } else { format = DateFormat.getMediumDateFormat(getContext()); } if (format instanceof SimpleDateFormat) { order = ((SimpleDateFormat) format).toPattern(); } else { // Shouldn't happen, but just in case. order = new String(DateFormat.getDateFormatOrder(getContext())); } /* Remove the 3 pickers from their parent and then add them back in the Loading @@ -176,14 +192,40 @@ public class DatePicker extends FrameLayout { */ LinearLayout parent = (LinearLayout) findViewById(R.id.parent); parent.removeAllViews(); for (char c : order) { if (c == DateFormat.DATE) { boolean quoted = false; boolean didDay = false, didMonth = false, didYear = false; for (int i = 0; i < order.length(); i++) { char c = order.charAt(i); if (c == '\'') { quoted = !quoted; } if (!quoted) { if (c == DateFormat.DATE && !didDay) { parent.addView(mDayPicker); } else if (c == DateFormat.MONTH) { didDay = true; } else if ((c == DateFormat.MONTH || c == 'L') && !didMonth) { parent.addView(mMonthPicker); } else { didMonth = true; } else if (c == DateFormat.YEAR && !didYear) { parent.addView (mYearPicker); didYear = true; } } } // Shouldn't happen, but just in case. if (!didMonth) { parent.addView(mMonthPicker); } if (!didDay) { parent.addView(mDayPicker); } if (!didYear) { parent.addView(mYearPicker); } } Loading @@ -192,6 +234,7 @@ public class DatePicker extends FrameLayout { mMonth = monthOfYear; mDay = dayOfMonth; updateSpinners(); reorderPickers(new DateFormatSymbols().getShortMonths()); } private static class SavedState extends BaseSavedState { Loading