The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.text.format; |
| 18 | |
Joachim Sauer | 20c5ef7 | 2017-06-01 11:22:26 +0100 | [diff] [blame] | 19 | import android.annotation.NonNull; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 21 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.provider.Settings; |
| 23 | import android.text.SpannableStringBuilder; |
| 24 | import android.text.Spanned; |
| 25 | import android.text.SpannedString; |
| 26 | |
Aurimas Liutikas | 4037d51 | 2016-10-11 17:20:06 -0700 | [diff] [blame] | 27 | import libcore.icu.ICU; |
| 28 | import libcore.icu.LocaleData; |
| 29 | |
| 30 | import java.text.SimpleDateFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import java.util.Calendar; |
| 32 | import java.util.Date; |
| 33 | import java.util.GregorianCalendar; |
| 34 | import java.util.Locale; |
| 35 | import java.util.TimeZone; |
Elliott Hughes | 4caba61 | 2013-01-14 15:48:27 -0800 | [diff] [blame] | 36 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | /** |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 38 | * Utility class for producing strings with formatted date/time. |
| 39 | * |
| 40 | * <p>Most callers should avoid supplying their own format strings to this |
| 41 | * class' {@code format} methods and rely on the correctly localized ones |
| 42 | * supplied by the system. This class' factory methods return |
| 43 | * appropriately-localized {@link java.text.DateFormat} instances, suitable |
| 44 | * for both formatting and parsing dates. For the canonical documentation |
| 45 | * of format strings, see {@link java.text.SimpleDateFormat}. |
| 46 | * |
Elliott Hughes | 031b581 | 2013-04-02 11:56:23 -0700 | [diff] [blame] | 47 | * <p>In cases where the system does not provide a suitable pattern, |
| 48 | * this class offers the {@link #getBestDateTimePattern} method. |
| 49 | * |
Elliott Hughes | fc55c2b | 2013-03-18 14:59:59 -0700 | [diff] [blame] | 50 | * <p>The {@code format} methods in this class implement a subset of Unicode |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 51 | * <a href="http://www.unicode.org/reports/tr35/#Date_Format_Patterns">UTS #35</a> patterns. |
Elliott Hughes | fc55c2b | 2013-03-18 14:59:59 -0700 | [diff] [blame] | 52 | * The subset currently supported by this class includes the following format characters: |
| 53 | * {@code acdEHhLKkLMmsyz}. Up to API level 17, only {@code adEhkMmszy} were supported. |
| 54 | * Note that this class incorrectly implements {@code k} as if it were {@code H} for backwards |
| 55 | * compatibility. |
| 56 | * |
| 57 | * <p>See {@link java.text.SimpleDateFormat} for more documentation |
| 58 | * about patterns, or if you need a more complete or correct implementation. |
| 59 | * Note that the non-{@code format} methods in this class are implemented by |
| 60 | * {@code SimpleDateFormat}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | public class DateFormat { |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 63 | /** |
| 64 | * @deprecated Use a literal {@code '} instead. |
| 65 | * @removed |
| 66 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 67 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | public static final char QUOTE = '\''; |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 69 | |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 70 | /** |
| 71 | * @deprecated Use a literal {@code 'a'} instead. |
| 72 | * @removed |
| 73 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 74 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | public static final char AM_PM = 'a'; |
| 76 | |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 77 | /** |
| 78 | * @deprecated Use a literal {@code 'a'} instead; 'A' was always equivalent to 'a'. |
| 79 | * @removed |
| 80 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 81 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | public static final char CAPITAL_AM_PM = 'A'; |
| 83 | |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 84 | /** |
| 85 | * @deprecated Use a literal {@code 'd'} instead. |
| 86 | * @removed |
| 87 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 88 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | public static final char DATE = 'd'; |
| 90 | |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 91 | /** |
| 92 | * @deprecated Use a literal {@code 'E'} instead. |
| 93 | * @removed |
| 94 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 95 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | public static final char DAY = 'E'; |
| 97 | |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 98 | /** |
| 99 | * @deprecated Use a literal {@code 'h'} instead. |
| 100 | * @removed |
| 101 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 102 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | public static final char HOUR = 'h'; |
| 104 | |
Elliott Hughes | fc55c2b | 2013-03-18 14:59:59 -0700 | [diff] [blame] | 105 | /** |
| 106 | * @deprecated Use a literal {@code 'H'} (for compatibility with {@link SimpleDateFormat} |
| 107 | * and Unicode) or {@code 'k'} (for compatibility with Android releases up to and including |
| 108 | * Jelly Bean MR-1) instead. Note that the two are incompatible. |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 109 | * |
| 110 | * @removed |
Elliott Hughes | fc55c2b | 2013-03-18 14:59:59 -0700 | [diff] [blame] | 111 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 112 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | public static final char HOUR_OF_DAY = 'k'; |
| 114 | |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 115 | /** |
| 116 | * @deprecated Use a literal {@code 'm'} instead. |
| 117 | * @removed |
| 118 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 119 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | public static final char MINUTE = 'm'; |
| 121 | |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 122 | /** |
| 123 | * @deprecated Use a literal {@code 'M'} instead. |
| 124 | * @removed |
| 125 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 126 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | public static final char MONTH = 'M'; |
| 128 | |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 129 | /** |
| 130 | * @deprecated Use a literal {@code 'L'} instead. |
| 131 | * @removed |
| 132 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 133 | @Deprecated |
Elliott Hughes | 34de3bc | 2012-09-14 21:10:05 -0700 | [diff] [blame] | 134 | public static final char STANDALONE_MONTH = 'L'; |
| 135 | |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 136 | /** |
| 137 | * @deprecated Use a literal {@code 's'} instead. |
| 138 | * @removed |
| 139 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 140 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | public static final char SECONDS = 's'; |
| 142 | |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 143 | /** |
| 144 | * @deprecated Use a literal {@code 'z'} instead. |
| 145 | * @removed |
| 146 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 147 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | public static final char TIME_ZONE = 'z'; |
| 149 | |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 150 | /** |
| 151 | * @deprecated Use a literal {@code 'y'} instead. |
| 152 | * @removed |
| 153 | */ |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 154 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | public static final char YEAR = 'y'; |
| 156 | |
| 157 | |
| 158 | private static final Object sLocaleLock = new Object(); |
| 159 | private static Locale sIs24HourLocale; |
| 160 | private static boolean sIs24Hour; |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * Returns true if user preference is set to 24-hour format. |
| 165 | * @param context the context to use for the content resolver |
| 166 | * @return true if 24 hour time format is selected, false otherwise. |
| 167 | */ |
| 168 | public static boolean is24HourFormat(Context context) { |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 169 | return is24HourFormat(context, UserHandle.myUserId()); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns true if user preference with the given user handle is set to 24-hour format. |
| 174 | * @param context the context to use for the content resolver |
| 175 | * @param userHandle the user handle of the user to query. |
| 176 | * @return true if 24 hour time format is selected, false otherwise. |
| 177 | * |
| 178 | * @hide |
| 179 | */ |
| 180 | public static boolean is24HourFormat(Context context, int userHandle) { |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 181 | final String value = Settings.System.getStringForUser(context.getContentResolver(), |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 182 | Settings.System.TIME_12_24, userHandle); |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 183 | if (value != null) { |
| 184 | return value.equals("24"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Joachim Sauer | 20c5ef7 | 2017-06-01 11:22:26 +0100 | [diff] [blame] | 187 | return is24HourLocale(context.getResources().getConfiguration().locale); |
| 188 | } |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 189 | |
Joachim Sauer | 20c5ef7 | 2017-06-01 11:22:26 +0100 | [diff] [blame] | 190 | /** |
| 191 | * Returns true if the specified locale uses a 24-hour time format by default, ignoring user |
| 192 | * settings. |
| 193 | * @param locale the locale to check |
| 194 | * @return true if the locale uses a 24 hour time format by default, false otherwise |
| 195 | * @hide |
| 196 | */ |
| 197 | public static boolean is24HourLocale(@NonNull Locale locale) { |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 198 | synchronized (sLocaleLock) { |
| 199 | if (sIs24HourLocale != null && sIs24HourLocale.equals(locale)) { |
| 200 | return sIs24Hour; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | final java.text.DateFormat natural = |
| 205 | java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale); |
| 206 | |
| 207 | final boolean is24Hour; |
| 208 | if (natural instanceof SimpleDateFormat) { |
| 209 | final SimpleDateFormat sdf = (SimpleDateFormat) natural; |
| 210 | final String pattern = sdf.toPattern(); |
| 211 | is24Hour = hasDesignator(pattern, 'H'); |
| 212 | } else { |
| 213 | is24Hour = false; |
| 214 | } |
| 215 | |
| 216 | synchronized (sLocaleLock) { |
| 217 | sIs24HourLocale = locale; |
| 218 | sIs24Hour = is24Hour; |
| 219 | } |
| 220 | |
| 221 | return is24Hour; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
Elliott Hughes | 031b581 | 2013-04-02 11:56:23 -0700 | [diff] [blame] | 225 | * Returns the best possible localized form of the given skeleton for the given |
| 226 | * locale. A skeleton is similar to, and uses the same format characters as, a Unicode |
| 227 | * <a href="http://www.unicode.org/reports/tr35/#Date_Format_Patterns">UTS #35</a> |
| 228 | * pattern. |
| 229 | * |
| 230 | * <p>One difference is that order is irrelevant. For example, "MMMMd" will return |
| 231 | * "MMMM d" in the {@code en_US} locale, but "d. MMMM" in the {@code de_CH} locale. |
| 232 | * |
| 233 | * <p>Note also in that second example that the necessary punctuation for German was |
| 234 | * added. For the same input in {@code es_ES}, we'd have even more extra text: |
| 235 | * "d 'de' MMMM". |
| 236 | * |
| 237 | * <p>This method will automatically correct for grammatical necessity. Given the |
| 238 | * same "MMMMd" input, this method will return "d LLLL" in the {@code fa_IR} locale, |
| 239 | * where stand-alone months are necessary. Lengths are preserved where meaningful, |
| 240 | * so "Md" would give a different result to "MMMd", say, except in a locale such as |
| 241 | * {@code ja_JP} where there is only one length of month. |
| 242 | * |
| 243 | * <p>This method will only return patterns that are in CLDR, and is useful whenever |
| 244 | * you know what elements you want in your format string but don't want to make your |
| 245 | * code specific to any one locale. |
| 246 | * |
| 247 | * @param locale the locale into which the skeleton should be localized |
| 248 | * @param skeleton a skeleton as described above |
| 249 | * @return a string pattern suitable for use with {@link java.text.SimpleDateFormat}. |
| 250 | */ |
| 251 | public static String getBestDateTimePattern(Locale locale, String skeleton) { |
Narayan Kamath | 2c9d200 | 2014-06-12 13:42:05 +0100 | [diff] [blame] | 252 | return ICU.getBestDateTimePattern(skeleton, locale); |
Elliott Hughes | 031b581 | 2013-04-02 11:56:23 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | * Returns a {@link java.text.DateFormat} object that can format the time according |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 257 | * to the context's locale and the user's 12-/24-hour clock preference. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | * @param context the application context |
| 259 | * @return the {@link java.text.DateFormat} object that properly formats the time. |
| 260 | */ |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 261 | public static java.text.DateFormat getTimeFormat(Context context) { |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 262 | final Locale locale = context.getResources().getConfiguration().locale; |
| 263 | return new java.text.SimpleDateFormat(getTimeFormatString(context), locale); |
Elliott Hughes | cdafd37 | 2013-03-18 17:21:33 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Returns a String pattern that can be used to format the time according |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 268 | * to the context's locale and the user's 12-/24-hour clock preference. |
Elliott Hughes | cdafd37 | 2013-03-18 17:21:33 -0700 | [diff] [blame] | 269 | * @param context the application context |
| 270 | * @hide |
| 271 | */ |
| 272 | public static String getTimeFormatString(Context context) { |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 273 | return getTimeFormatString(context, UserHandle.myUserId()); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Returns a String pattern that can be used to format the time according |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 278 | * to the context's locale and the user's 12-/24-hour clock preference. |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 279 | * @param context the application context |
| 280 | * @param userHandle the user handle of the user to query the format for |
| 281 | * @hide |
| 282 | */ |
| 283 | public static String getTimeFormatString(Context context, int userHandle) { |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 284 | final LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); |
Jorim Jaggi | 0da89b7 | 2014-11-25 15:34:56 +0100 | [diff] [blame] | 285 | return is24HourFormat(context, userHandle) ? d.timeFormat_Hm : d.timeFormat_hm; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 289 | * Returns a {@link java.text.DateFormat} object that can format the date |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 290 | * in short form according to the context's locale. |
Narayan Kamath | f91f06a | 2014-11-18 13:23:02 +0000 | [diff] [blame] | 291 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | * @param context the application context |
| 293 | * @return the {@link java.text.DateFormat} object that properly formats the date. |
| 294 | */ |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 295 | public static java.text.DateFormat getDateFormat(Context context) { |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 296 | final Locale locale = context.getResources().getConfiguration().locale; |
| 297 | return java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, locale); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | } |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 299 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | /** |
| 301 | * Returns a {@link java.text.DateFormat} object that can format the date |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 302 | * in long form (such as {@code Monday, January 3, 2000}) for the context's locale. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | * @param context the application context |
| 304 | * @return the {@link java.text.DateFormat} object that formats the date in long form. |
| 305 | */ |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 306 | public static java.text.DateFormat getLongDateFormat(Context context) { |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 307 | final Locale locale = context.getResources().getConfiguration().locale; |
| 308 | return java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG, locale); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Returns a {@link java.text.DateFormat} object that can format the date |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 313 | * in medium form (such as {@code Jan 3, 2000}) for the context's locale. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | * @param context the application context |
| 315 | * @return the {@link java.text.DateFormat} object that formats the date in long form. |
| 316 | */ |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 317 | public static java.text.DateFormat getMediumDateFormat(Context context) { |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 318 | final Locale locale = context.getResources().getConfiguration().locale; |
| 319 | return java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM, locale); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
Narayan Kamath | 9d68b3c | 2014-10-22 12:55:58 +0100 | [diff] [blame] | 323 | * Gets the current date format stored as a char array. Returns a 3 element |
| 324 | * array containing the day ({@code 'd'}), month ({@code 'M'}), and year ({@code 'y'})) |
| 325 | * in the order specified by the user's format preference. Note that this order is |
Elliott Hughes | edd6f9e | 2013-04-30 11:09:53 -0700 | [diff] [blame] | 326 | * <i>only</i> appropriate for all-numeric dates; spelled-out (MEDIUM and LONG) |
Eric Fischer | 03a8017 | 2009-07-23 18:32:42 -0700 | [diff] [blame] | 327 | * dates will generally contain other punctuation, spaces, or words, |
| 328 | * not just the day, month, and year, and not necessarily in the same |
| 329 | * order returned here. |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 330 | */ |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 331 | public static char[] getDateFormatOrder(Context context) { |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 332 | return ICU.getDateFormatOrder(getDateFormatString(context)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | } |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 334 | |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 335 | private static String getDateFormatString(Context context) { |
| 336 | final Locale locale = context.getResources().getConfiguration().locale; |
| 337 | java.text.DateFormat df = java.text.DateFormat.getDateInstance( |
| 338 | java.text.DateFormat.SHORT, locale); |
Narayan Kamath | f91f06a | 2014-11-18 13:23:02 +0000 | [diff] [blame] | 339 | if (df instanceof SimpleDateFormat) { |
| 340 | return ((SimpleDateFormat) df).toPattern(); |
| 341 | } |
Eric Fischer | 03a8017 | 2009-07-23 18:32:42 -0700 | [diff] [blame] | 342 | |
Narayan Kamath | f91f06a | 2014-11-18 13:23:02 +0000 | [diff] [blame] | 343 | throw new AssertionError("!(df instanceof SimpleDateFormat)"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 347 | * Given a format string and a time in milliseconds since Jan 1, 1970 GMT, returns a |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | * CharSequence containing the requested date. |
| 349 | * @param inFormat the format string, as described in {@link android.text.format.DateFormat} |
| 350 | * @param inTimeInMillis in milliseconds since Jan 1, 1970 GMT |
| 351 | * @return a {@link CharSequence} containing the requested text |
| 352 | */ |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 353 | public static CharSequence format(CharSequence inFormat, long inTimeInMillis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | return format(inFormat, new Date(inTimeInMillis)); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Given a format string and a {@link java.util.Date} object, returns a CharSequence containing |
| 359 | * the requested date. |
| 360 | * @param inFormat the format string, as described in {@link android.text.format.DateFormat} |
| 361 | * @param inDate the date to format |
| 362 | * @return a {@link CharSequence} containing the requested text |
| 363 | */ |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 364 | public static CharSequence format(CharSequence inFormat, Date inDate) { |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 365 | Calendar c = new GregorianCalendar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | c.setTime(inDate); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | return format(inFormat, c); |
| 368 | } |
| 369 | |
| 370 | /** |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 371 | * Indicates whether the specified format string contains seconds. |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 372 | * |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 373 | * Always returns false if the input format is null. |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 374 | * |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 375 | * @param inFormat the format string, as described in {@link android.text.format.DateFormat} |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 376 | * |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 377 | * @return true if the format string contains {@link #SECONDS}, false otherwise |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 378 | * |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 379 | * @hide |
| 380 | */ |
| 381 | public static boolean hasSeconds(CharSequence inFormat) { |
Jeff Sharkey | 06c5f8a | 2012-12-04 09:53:44 -0800 | [diff] [blame] | 382 | return hasDesignator(inFormat, SECONDS); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Test if a format string contains the given designator. Always returns |
| 387 | * {@code false} if the input format is {@code null}. |
| 388 | * |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 389 | * Note that this is intended for searching for designators, not arbitrary |
| 390 | * characters. So searching for a literal single quote would not work correctly. |
| 391 | * |
Jeff Sharkey | 06c5f8a | 2012-12-04 09:53:44 -0800 | [diff] [blame] | 392 | * @hide |
| 393 | */ |
| 394 | public static boolean hasDesignator(CharSequence inFormat, char designator) { |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 395 | if (inFormat == null) return false; |
| 396 | |
| 397 | final int length = inFormat.length(); |
| 398 | |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 399 | boolean insideQuote = false; |
| 400 | for (int i = 0; i < length; i++) { |
| 401 | final char c = inFormat.charAt(i); |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 402 | if (c == QUOTE) { |
Roozbeh Pournader | 3150434 | 2017-07-20 12:34:06 -0700 | [diff] [blame] | 403 | insideQuote = !insideQuote; |
| 404 | } else if (!insideQuote) { |
| 405 | if (c == designator) { |
| 406 | return true; |
| 407 | } |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | return false; |
| 412 | } |
| 413 | |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 414 | /** |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 415 | * Given a format string and a {@link java.util.Calendar} object, returns a CharSequence |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | * containing the requested date. |
| 417 | * @param inFormat the format string, as described in {@link android.text.format.DateFormat} |
| 418 | * @param inDate the date to format |
| 419 | * @return a {@link CharSequence} containing the requested text |
| 420 | */ |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 421 | public static CharSequence format(CharSequence inFormat, Calendar inDate) { |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 422 | SpannableStringBuilder s = new SpannableStringBuilder(inFormat); |
| 423 | int count; |
| 424 | |
| 425 | LocaleData localeData = LocaleData.get(Locale.getDefault()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | |
| 427 | int len = inFormat.length(); |
| 428 | |
| 429 | for (int i = 0; i < len; i += count) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | count = 1; |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 431 | int c = s.charAt(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | |
| 433 | if (c == QUOTE) { |
| 434 | count = appendQuotedText(s, i, len); |
| 435 | len = s.length(); |
| 436 | continue; |
| 437 | } |
| 438 | |
| 439 | while ((i + count < len) && (s.charAt(i + count) == c)) { |
| 440 | count++; |
| 441 | } |
| 442 | |
| 443 | String replacement; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | switch (c) { |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 445 | case 'A': |
| 446 | case 'a': |
| 447 | replacement = localeData.amPm[inDate.get(Calendar.AM_PM) - Calendar.AM]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | break; |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 449 | case 'd': |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | replacement = zeroPad(inDate.get(Calendar.DATE), count); |
| 451 | break; |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 452 | case 'c': |
| 453 | case 'E': |
| 454 | replacement = getDayOfWeekString(localeData, |
| 455 | inDate.get(Calendar.DAY_OF_WEEK), count, c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | break; |
Elliott Hughes | 7a89f62 | 2013-03-11 14:49:44 -0700 | [diff] [blame] | 457 | case 'K': // hour in am/pm (0-11) |
| 458 | case 'h': // hour in am/pm (1-12) |
| 459 | { |
| 460 | int hour = inDate.get(Calendar.HOUR); |
| 461 | if (c == 'h' && hour == 0) { |
| 462 | hour = 12; |
| 463 | } |
| 464 | replacement = zeroPad(hour, count); |
| 465 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | break; |
Elliott Hughes | 7a89f62 | 2013-03-11 14:49:44 -0700 | [diff] [blame] | 467 | case 'H': // hour in day (0-23) |
Elliott Hughes | fc55c2b | 2013-03-18 14:59:59 -0700 | [diff] [blame] | 468 | case 'k': // hour in day (1-24) [but see note below] |
Elliott Hughes | 7a89f62 | 2013-03-11 14:49:44 -0700 | [diff] [blame] | 469 | { |
| 470 | int hour = inDate.get(Calendar.HOUR_OF_DAY); |
Elliott Hughes | fc55c2b | 2013-03-18 14:59:59 -0700 | [diff] [blame] | 471 | // Historically on Android 'k' was interpreted as 'H', which wasn't |
| 472 | // implemented, so pretty much all callers that want to format 24-hour |
| 473 | // times are abusing 'k'. http://b/8359981. |
| 474 | if (false && c == 'k' && hour == 0) { |
Elliott Hughes | 7a89f62 | 2013-03-11 14:49:44 -0700 | [diff] [blame] | 475 | hour = 24; |
| 476 | } |
| 477 | replacement = zeroPad(hour, count); |
| 478 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | break; |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 480 | case 'L': |
| 481 | case 'M': |
| 482 | replacement = getMonthString(localeData, |
| 483 | inDate.get(Calendar.MONTH), count, c); |
| 484 | break; |
| 485 | case 'm': |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | replacement = zeroPad(inDate.get(Calendar.MINUTE), count); |
| 487 | break; |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 488 | case 's': |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | replacement = zeroPad(inDate.get(Calendar.SECOND), count); |
| 490 | break; |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 491 | case 'y': |
| 492 | replacement = getYearString(inDate.get(Calendar.YEAR), count); |
| 493 | break; |
| 494 | case 'z': |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | replacement = getTimeZoneString(inDate, count); |
| 496 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | default: |
| 498 | replacement = null; |
| 499 | break; |
| 500 | } |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 501 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | if (replacement != null) { |
| 503 | s.replace(i, i + count, replacement); |
| 504 | count = replacement.length(); // CARE: count is used in the for loop above |
| 505 | len = s.length(); |
| 506 | } |
| 507 | } |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 508 | |
| 509 | if (inFormat instanceof Spanned) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | return new SpannedString(s); |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 511 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | return s.toString(); |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 513 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | } |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 515 | |
| 516 | private static String getDayOfWeekString(LocaleData ld, int day, int count, int kind) { |
| 517 | boolean standalone = (kind == 'c'); |
| 518 | if (count == 5) { |
| 519 | return standalone ? ld.tinyStandAloneWeekdayNames[day] : ld.tinyWeekdayNames[day]; |
| 520 | } else if (count == 4) { |
| 521 | return standalone ? ld.longStandAloneWeekdayNames[day] : ld.longWeekdayNames[day]; |
| 522 | } else { |
| 523 | return standalone ? ld.shortStandAloneWeekdayNames[day] : ld.shortWeekdayNames[day]; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | private static String getMonthString(LocaleData ld, int month, int count, int kind) { |
| 528 | boolean standalone = (kind == 'L'); |
| 529 | if (count == 5) { |
| 530 | return standalone ? ld.tinyStandAloneMonthNames[month] : ld.tinyMonthNames[month]; |
| 531 | } else if (count == 4) { |
| 532 | return standalone ? ld.longStandAloneMonthNames[month] : ld.longMonthNames[month]; |
Elliott Hughes | 34de3bc | 2012-09-14 21:10:05 -0700 | [diff] [blame] | 533 | } else if (count == 3) { |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 534 | return standalone ? ld.shortStandAloneMonthNames[month] : ld.shortMonthNames[month]; |
Elliott Hughes | 34de3bc | 2012-09-14 21:10:05 -0700 | [diff] [blame] | 535 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | // Calendar.JANUARY == 0, so add 1 to month. |
| 537 | return zeroPad(month+1, count); |
| 538 | } |
| 539 | } |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 540 | |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 541 | private static String getTimeZoneString(Calendar inDate, int count) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | TimeZone tz = inDate.getTimeZone(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | if (count < 2) { // FIXME: shouldn't this be <= 2 ? |
| 544 | return formatZoneOffset(inDate.get(Calendar.DST_OFFSET) + |
| 545 | inDate.get(Calendar.ZONE_OFFSET), |
| 546 | count); |
| 547 | } else { |
| 548 | boolean dst = inDate.get(Calendar.DST_OFFSET) != 0; |
| 549 | return tz.getDisplayName(dst, TimeZone.SHORT); |
| 550 | } |
| 551 | } |
| 552 | |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 553 | private static String formatZoneOffset(int offset, int count) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | offset /= 1000; // milliseconds to seconds |
| 555 | StringBuilder tb = new StringBuilder(); |
| 556 | |
| 557 | if (offset < 0) { |
| 558 | tb.insert(0, "-"); |
| 559 | offset = -offset; |
| 560 | } else { |
| 561 | tb.insert(0, "+"); |
| 562 | } |
| 563 | |
| 564 | int hours = offset / 3600; |
| 565 | int minutes = (offset % 3600) / 60; |
| 566 | |
| 567 | tb.append(zeroPad(hours, 2)); |
| 568 | tb.append(zeroPad(minutes, 2)); |
| 569 | return tb.toString(); |
| 570 | } |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 571 | |
| 572 | private static String getYearString(int year, int count) { |
Elliott Hughes | 34de3bc | 2012-09-14 21:10:05 -0700 | [diff] [blame] | 573 | return (count <= 2) ? zeroPad(year % 100, 2) |
| 574 | : String.format(Locale.getDefault(), "%d", year); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | } |
Elliott Hughes | 8326b9a | 2013-03-08 15:06:14 -0800 | [diff] [blame] | 576 | |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 577 | private static int appendQuotedText(SpannableStringBuilder s, int i, int len) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | if (i + 1 < len && s.charAt(i + 1) == QUOTE) { |
| 579 | s.delete(i, i + 1); |
| 580 | return 1; |
| 581 | } |
| 582 | |
| 583 | int count = 0; |
| 584 | |
| 585 | // delete leading quote |
| 586 | s.delete(i, i + 1); |
| 587 | len--; |
| 588 | |
| 589 | while (i < len) { |
| 590 | char c = s.charAt(i); |
| 591 | |
| 592 | if (c == QUOTE) { |
| 593 | // QUOTEQUOTE -> QUOTE |
| 594 | if (i + 1 < len && s.charAt(i + 1) == QUOTE) { |
| 595 | |
| 596 | s.delete(i, i + 1); |
| 597 | len--; |
| 598 | count++; |
| 599 | i++; |
| 600 | } else { |
| 601 | // Closing QUOTE ends quoted text copying |
| 602 | s.delete(i, i + 1); |
| 603 | break; |
| 604 | } |
| 605 | } else { |
| 606 | i++; |
| 607 | count++; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | return count; |
| 612 | } |
| 613 | |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 614 | private static String zeroPad(int inValue, int inMinDigits) { |
Elliott Hughes | 34de3bc | 2012-09-14 21:10:05 -0700 | [diff] [blame] | 615 | return String.format(Locale.getDefault(), "%0" + inMinDigits + "d", inValue); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | } |
| 617 | } |