summaryrefslogtreecommitdiffstats
path: root/core/java/android/text
diff options
context:
space:
mode:
authorEric Fischer <enf@google.com>2009-05-12 16:29:46 -0700
committerEric Fischer <enf@google.com>2009-05-15 10:15:50 -0700
commit5bd644caf73e76750feef1a82b8817d32f5367fc (patch)
tree345fac5c8625cb5ce037613fec1ab5015b7e7fd6 /core/java/android/text
parent4535e40544aeb957d44fad75fbe5676effe03689 (diff)
downloadframeworks_base-5bd644caf73e76750feef1a82b8817d32f5367fc.zip
frameworks_base-5bd644caf73e76750feef1a82b8817d32f5367fc.tar.gz
frameworks_base-5bd644caf73e76750feef1a82b8817d32f5367fc.tar.bz2
Start using CLDR for some date-and-time strings and formats.
Use java.text.DateFormat where possible, since that is already using the CLDR data for the things it supports. Remove an unused date format object from DatePickerDialog. Add a new method for getting the standalone month names from applications, although @hidden for now because it is an API change. Pass the standalone month names down to native code in Time so that tztime's strftime() can use them. And then the bulk of the change: replace all the names for the months and the days of the week, and AM and PM, and yesterday, today, and tomorrow, with strings from CLDR. And replace several of the date format strings with ones derived from CLDR, but reformatted to use strftime() style instead of SimpleDateFormat style. All these resource changes go into new donottranslate-cldr.xml files and are removed from strings.xml so that they aren't part of the normal translation process and the translators don't have to bother with them (and risk messing them up).
Diffstat (limited to 'core/java/android/text')
-rw-r--r--core/java/android/text/format/DateFormat.java50
-rw-r--r--core/java/android/text/format/DateUtils.java100
-rw-r--r--core/java/android/text/format/Time.java15
3 files changed, 105 insertions, 60 deletions
diff --git a/core/java/android/text/format/DateFormat.java b/core/java/android/text/format/DateFormat.java
index 0dc96c3..3156d8b 100644
--- a/core/java/android/text/format/DateFormat.java
+++ b/core/java/android/text/format/DateFormat.java
@@ -242,64 +242,48 @@ public class DateFormat {
/**
* Returns a {@link java.text.DateFormat} object that can format the time according
- * to the current user preference.
+ * to the current locale.
* @param context the application context
* @return the {@link java.text.DateFormat} object that properly formats the time.
*/
public static final java.text.DateFormat getTimeFormat(Context context) {
- boolean b24 = is24HourFormat(context);
- int res;
-
- if (b24) {
- res = R.string.twenty_four_hour_time_format;
- } else {
- res = R.string.twelve_hour_time_format;
- }
-
- return new java.text.SimpleDateFormat(context.getString(res));
+ return java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT);
}
/**
- * Returns a {@link java.text.DateFormat} object that can format the date according
- * to the current user preference.
+ * Returns a {@link java.text.DateFormat} object that can format the date
+ * in short form (such as 12/31/1999) according
+ * to the current locale.
* @param context the application context
* @return the {@link java.text.DateFormat} object that properly formats the date.
*/
public static final java.text.DateFormat getDateFormat(Context context) {
- String value = getDateFormatString(context);
+ /*
+ * We use a resource string here instead of just DateFormat.SHORT
+ * so that we get a four-digit year instead a two-digit year.
+ */
+ String value = context.getString(R.string.numeric_date_format);
return new java.text.SimpleDateFormat(value);
}
/**
* Returns a {@link java.text.DateFormat} object that can format the date
- * in long form (such as December 31, 1999) based on user preference.
+ * in long form (such as December 31, 1999) for the current locale.
* @param context the application context
* @return the {@link java.text.DateFormat} object that formats the date in long form.
*/
public static final java.text.DateFormat getLongDateFormat(Context context) {
- String value = getDateFormatString(context);
- if (value.indexOf('M') < value.indexOf('d')) {
- value = context.getString(R.string.full_date_month_first);
- } else {
- value = context.getString(R.string.full_date_day_first);
- }
- return new java.text.SimpleDateFormat(value);
+ return java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG);
}
/**
* Returns a {@link java.text.DateFormat} object that can format the date
- * in medium form (such as Dec. 31, 1999) based on user preference.
+ * in medium form (such as Dec. 31, 1999) for the current locale.
* @param context the application context
* @return the {@link java.text.DateFormat} object that formats the date in long form.
*/
public static final java.text.DateFormat getMediumDateFormat(Context context) {
- String value = getDateFormatString(context);
- if (value.indexOf('M') < value.indexOf('d')) {
- value = context.getString(R.string.medium_date_month_first);
- } else {
- value = context.getString(R.string.medium_date_day_first);
- }
- return new java.text.SimpleDateFormat(value);
+ return java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM);
}
/**
@@ -338,6 +322,12 @@ 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) {
diff --git a/core/java/android/text/format/DateUtils.java b/core/java/android/text/format/DateUtils.java
index 8a7cdd9..9073d82 100644
--- a/core/java/android/text/format/DateUtils.java
+++ b/core/java/android/text/format/DateUtils.java
@@ -62,15 +62,6 @@ public class DateUtils
com.android.internal.R.string.day_of_week_short_friday,
com.android.internal.R.string.day_of_week_short_saturday,
};
- private static final int[] sDaysShorter = new int[] {
- com.android.internal.R.string.day_of_week_shorter_sunday,
- com.android.internal.R.string.day_of_week_shorter_monday,
- com.android.internal.R.string.day_of_week_shorter_tuesday,
- com.android.internal.R.string.day_of_week_shorter_wednesday,
- com.android.internal.R.string.day_of_week_shorter_thursday,
- com.android.internal.R.string.day_of_week_shorter_friday,
- com.android.internal.R.string.day_of_week_shorter_saturday,
- };
private static final int[] sDaysShortest = new int[] {
com.android.internal.R.string.day_of_week_shortest_sunday,
com.android.internal.R.string.day_of_week_shortest_monday,
@@ -80,6 +71,20 @@ public class DateUtils
com.android.internal.R.string.day_of_week_shortest_friday,
com.android.internal.R.string.day_of_week_shortest_saturday,
};
+ private static final int[] sMonthsStandaloneLong = new int [] {
+ com.android.internal.R.string.month_long_standalone_january,
+ com.android.internal.R.string.month_long_standalone_february,
+ com.android.internal.R.string.month_long_standalone_march,
+ com.android.internal.R.string.month_long_standalone_april,
+ com.android.internal.R.string.month_long_standalone_may,
+ com.android.internal.R.string.month_long_standalone_june,
+ com.android.internal.R.string.month_long_standalone_july,
+ com.android.internal.R.string.month_long_standalone_august,
+ com.android.internal.R.string.month_long_standalone_september,
+ com.android.internal.R.string.month_long_standalone_october,
+ com.android.internal.R.string.month_long_standalone_november,
+ com.android.internal.R.string.month_long_standalone_december,
+ };
private static final int[] sMonthsLong = new int [] {
com.android.internal.R.string.month_long_january,
com.android.internal.R.string.month_long_february,
@@ -127,7 +132,7 @@ public class DateUtils
com.android.internal.R.string.pm,
};
private static Configuration sLastConfig;
- private static String sStatusTimeFormat;
+ private static java.text.DateFormat sStatusTimeFormat;
private static String sElapsedFormatMMSS;
private static String sElapsedFormatHMMSS;
@@ -255,18 +260,15 @@ public class DateUtils
* For use with the 'abbrev' parameter of {@link #getDayOfWeekString} and {@link #getMonthString}.
* @more
* <p>e.g. "Su" or "Jan"
- * <p>In some languages, the results returned for LENGTH_SHORT may be the same as
- * return for {@link #LENGTH_MEDIUM}.
+ * <p>In most languages, the results returned for LENGTH_SHORT will be the same as
+ * the results returned for {@link #LENGTH_MEDIUM}.
*/
public static final int LENGTH_SHORT = 30;
/**
* Request an even shorter abbreviated version of the name.
- * For use with the 'abbrev' parameter of {@link #getDayOfWeekString} and {@link #getMonthString}.
- * @more
- * <p>e.g. "M", "Tu", "Th" or "J"
- * <p>In some languages, the results returned for LENGTH_SHORTEST may be the same as
- * return for {@link #LENGTH_SHORTER}.
+ * Do not use this. Currently this will always return the same result
+ * as {@link #LENGTH_SHORT}.
*/
public static final int LENGTH_SHORTER = 40;
@@ -275,8 +277,8 @@ public class DateUtils
* For use with the 'abbrev' parameter of {@link #getDayOfWeekString} and {@link #getMonthString}.
* @more
* <p>e.g. "S", "T", "T" or "J"
- * <p>In some languages, the results returned for LENGTH_SHORTEST may be the same as
- * return for {@link #LENGTH_SHORTER}.
+ * <p>In some languages, the results returned for LENGTH_SHORTEST will be the same as
+ * the results returned for {@link #LENGTH_SHORT}.
*/
public static final int LENGTH_SHORTEST = 50;
@@ -284,9 +286,12 @@ public class DateUtils
* Return a string for the day of the week.
* @param dayOfWeek One of {@link Calendar#SUNDAY Calendar.SUNDAY},
* {@link Calendar#MONDAY Calendar.MONDAY}, etc.
- * @param abbrev One of {@link #LENGTH_LONG}, {@link #LENGTH_SHORT}, {@link #LENGTH_SHORTER}
- * or {@link #LENGTH_SHORTEST}. For forward compatibility, anything else
- * will return the same as {#LENGTH_MEDIUM}.
+ * @param abbrev One of {@link #LENGTH_LONG}, {@link #LENGTH_SHORT},
+ * {@link #LENGTH_MEDIUM}, or {@link #LENGTH_SHORTEST}.
+ * Note that in most languages, {@link #LENGTH_SHORT}
+ * will return the same as {@link #LENGTH_MEDIUM}.
+ * Undefined lengths will return {@link #LENGTH_MEDIUM}
+ * but may return something different in the future.
* @throws IndexOutOfBoundsException if the dayOfWeek is out of bounds.
*/
public static String getDayOfWeekString(int dayOfWeek, int abbrev) {
@@ -295,7 +300,7 @@ public class DateUtils
case LENGTH_LONG: list = sDaysLong; break;
case LENGTH_MEDIUM: list = sDaysMedium; break;
case LENGTH_SHORT: list = sDaysShort; break;
- case LENGTH_SHORTER: list = sDaysShorter; break;
+ case LENGTH_SHORTER: list = sDaysShort; break;
case LENGTH_SHORTEST: list = sDaysShortest; break;
default: list = sDaysMedium; break;
}
@@ -316,13 +321,14 @@ public class DateUtils
}
/**
- * Return a localized string for the day of the week.
+ * Return a localized string for the month of the year.
* @param month One of {@link Calendar#JANUARY Calendar.JANUARY},
* {@link Calendar#FEBRUARY Calendar.FEBRUARY}, etc.
- * @param abbrev One of {@link #LENGTH_LONG}, {@link #LENGTH_SHORT}, {@link #LENGTH_SHORTER}
- * or {@link #LENGTH_SHORTEST}. For forward compatibility, anything else
- * will return the same as {#LENGTH_MEDIUM}.
- * @return Localized day of the week.
+ * @param abbrev One of {@link #LENGTH_LONG}, {@link #LENGTH_MEDIUM},
+ * or {@link #LENGTH_SHORTEST}.
+ * Undefined lengths will return {@link #LENGTH_MEDIUM}
+ * but may return something different in the future.
+ * @return Localized month of the year.
*/
public static String getMonthString(int month, int abbrev) {
// Note that here we use sMonthsMedium for MEDIUM, SHORT and SHORTER.
@@ -344,6 +350,40 @@ public class DateUtils
}
/**
+ * Return a localized string for the month of the year, for
+ * contexts where the month is not formatted together with
+ * a day of the month.
+ *
+ * @param month One of {@link Calendar#JANUARY Calendar.JANUARY},
+ * {@link Calendar#FEBRUARY Calendar.FEBRUARY}, etc.
+ * @param abbrev One of {@link #LENGTH_LONG}, {@link #LENGTH_MEDIUM},
+ * or {@link #LENGTH_SHORTEST}.
+ * Undefined lengths will return {@link #LENGTH_MEDIUM}
+ * but may return something different in the future.
+ * @return Localized month of the year.
+ * @hide Pending API council approval
+ */
+ public static String getStandaloneMonthString(int month, int abbrev) {
+ // Note that here we use sMonthsMedium for MEDIUM, SHORT and SHORTER.
+ // This is a shortcut to not spam the translators with too many variations
+ // of the same string. If we find that in a language the distinction
+ // is necessary, we can can add more without changing this API.
+ int[] list;
+ switch (abbrev) {
+ case LENGTH_LONG: list = sMonthsStandaloneLong;
+ break;
+ case LENGTH_MEDIUM: list = sMonthsMedium; break;
+ case LENGTH_SHORT: list = sMonthsMedium; break;
+ case LENGTH_SHORTER: list = sMonthsMedium; break;
+ case LENGTH_SHORTEST: list = sMonthsShortest; break;
+ default: list = sMonthsMedium; break;
+ }
+
+ Resources r = Resources.getSystem();
+ return r.getString(list[month - Calendar.JANUARY]);
+ }
+
+ /**
* Returns a string describing the elapsed time since startTime.
* @param startTime some time in the past.
* @return a String object containing the elapsed time.
@@ -572,7 +612,7 @@ public class DateUtils
Configuration cfg = r.getConfiguration();
if (sLastConfig == null || !sLastConfig.equals(cfg)) {
sLastConfig = cfg;
- sStatusTimeFormat = r.getString(com.android.internal.R.string.status_bar_time_format);
+ sStatusTimeFormat = java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT);
sElapsedFormatMMSS = r.getString(com.android.internal.R.string.elapsed_time_short_format_mm_ss);
sElapsedFormatHMMSS = r.getString(com.android.internal.R.string.elapsed_time_short_format_h_mm_ss);
}
@@ -586,7 +626,7 @@ public class DateUtils
*/
public static final CharSequence timeString(long millis) {
initFormatStrings();
- return DateFormat.format(sStatusTimeFormat, millis);
+ return sStatusTimeFormat.format(millis);
}
/**
diff --git a/core/java/android/text/format/Time.java b/core/java/android/text/format/Time.java
index 8f12355..8eae111 100644
--- a/core/java/android/text/format/Time.java
+++ b/core/java/android/text/format/Time.java
@@ -135,6 +135,7 @@ public class Time {
private static Locale sLocale;
private static String[] sShortMonths;
private static String[] sLongMonths;
+ private static String[] sLongStandaloneMonths;
private static String[] sShortWeekdays;
private static String[] sLongWeekdays;
private static String sTimeOnlyFormat;
@@ -321,6 +322,20 @@ public class Time {
r.getString(com.android.internal.R.string.month_long_november),
r.getString(com.android.internal.R.string.month_long_december),
};
+ sLongStandaloneMonths = new String[] {
+ r.getString(com.android.internal.R.string.month_long_standalone_january),
+ r.getString(com.android.internal.R.string.month_long_standalone_february),
+ r.getString(com.android.internal.R.string.month_long_standalone_march),
+ r.getString(com.android.internal.R.string.month_long_standalone_april),
+ r.getString(com.android.internal.R.string.month_long_standalone_may),
+ r.getString(com.android.internal.R.string.month_long_standalone_june),
+ r.getString(com.android.internal.R.string.month_long_standalone_july),
+ r.getString(com.android.internal.R.string.month_long_standalone_august),
+ r.getString(com.android.internal.R.string.month_long_standalone_september),
+ r.getString(com.android.internal.R.string.month_long_standalone_october),
+ r.getString(com.android.internal.R.string.month_long_standalone_november),
+ r.getString(com.android.internal.R.string.month_long_standalone_december),
+ };
sShortWeekdays = new String[] {
r.getString(com.android.internal.R.string.day_of_week_medium_sunday),
r.getString(com.android.internal.R.string.day_of_week_medium_monday),