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