1f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes/* 2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one or more 3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * contributor license agreements. See the NOTICE file distributed with 4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * this work for additional information regarding copyright ownership. 5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The ASF licenses this file to You under the Apache License, Version 2.0 6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * (the "License"); you may not use this file except in compliance with 7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the License. You may obtain a copy of the License at 8f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 10f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software 12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * See the License for the specific language governing permissions and 15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * limitations under the License. 16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpackage java.text; 19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 20adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.InvalidObjectException; 21adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.util.Calendar; 22adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.util.Date; 23adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.util.Hashtable; 24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.util.Locale; 25adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.util.TimeZone; 26162b0775772fa66b7eb634760a8159a60c1ddceaElliott Hughesimport libcore.icu.ICU; 27162b0775772fa66b7eb634760a8159a60c1ddceaElliott Hughesimport libcore.icu.LocaleData; 28adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 29adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/** 30b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * Formats or parses dates and times. 319b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * 32b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * <p>This class provides factories for obtaining instances configured for a specific locale. 33b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * The most common subclass is {@link SimpleDateFormat}. 349b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * 35b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * <h4>Sample Code</h4> 36b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * <p>This code: 37adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <pre> 38b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * DateFormat[] formats = new DateFormat[] { 39b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * DateFormat.getDateInstance(), 40b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * DateFormat.getDateTimeInstance(), 41b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * DateFormat.getTimeInstance(), 42b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * }; 43b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * for (DateFormat df : formats) { 44b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * System.out.println(df.format(new Date(0))); 45b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * df.setTimeZone(TimeZone.getTimeZone("UTC")); 46b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * System.out.println(df.format(new Date(0))); 47adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * } 48adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </pre> 499b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * 50b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * <p>Produces this output when run on an {@code en_US} device in the America/Los_Angeles time zone: 51adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <pre> 52b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * Dec 31, 1969 53b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * Jan 1, 1970 54b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * Dec 31, 1969 4:00:00 PM 55b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * Jan 1, 1970 12:00:00 AM 56b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * 4:00:00 PM 57b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * 12:00:00 AM 58adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </pre> 59b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * And will produce similarly appropriate localized human-readable output on any user's system. 60b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * Notice how the same point in time when formatted can appear to be a different time when rendered 61b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * for a different time zone. This is one reason why formatting should be left until the data will 62b98632738dfc72ba32ae0160eada03a97a303288Elliott Hughes * only be presented to a human. Machines should interchange "Unix time" integers. 63adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 64adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpublic abstract class DateFormat extends Format { 65adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 66adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project private static final long serialVersionUID = 7218322306649953788L; 67adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 68adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 690836eb6118ddedbfe420b5d6ff98f07adf5a5d23Elliott Hughes * A tri-state boolean. If we're running stand-alone this will be null. 700836eb6118ddedbfe420b5d6ff98f07adf5a5d23Elliott Hughes * If we're running in an app, the frameworks will have told us the user preference. 710836eb6118ddedbfe420b5d6ff98f07adf5a5d23Elliott Hughes * @hide 720836eb6118ddedbfe420b5d6ff98f07adf5a5d23Elliott Hughes */ 730836eb6118ddedbfe420b5d6ff98f07adf5a5d23Elliott Hughes public static Boolean is24Hour; 740836eb6118ddedbfe420b5d6ff98f07adf5a5d23Elliott Hughes 750836eb6118ddedbfe420b5d6ff98f07adf5a5d23Elliott Hughes /** 76adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The calendar that this {@code DateFormat} uses to format a number 77adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * representing a date. 78adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 79adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project protected Calendar calendar; 80adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 81adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 82adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The number format used to format a number. 83adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 84adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project protected NumberFormat numberFormat; 85adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 86adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 87adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The format style constant defining the default format style. The default 88adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * is MEDIUM. 89adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 900d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int DEFAULT = 2; 91adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 92adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 93adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The format style constant defining the full style. 94adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 950d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int FULL = 0; 96adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 97adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 98adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The format style constant defining the long style. 99adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1000d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int LONG = 1; 101adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 102adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 103adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The format style constant defining the medium style. 104adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1050d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int MEDIUM = 2; 106adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 107adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 108adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The format style constant defining the short style. 109adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1100d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int SHORT = 3; 111adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 112adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 113adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The {@code FieldPosition} selector for 'G' field alignment, corresponds 114adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * to the {@link Calendar#ERA} field. 115adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1160d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int ERA_FIELD = 0; 117adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 118adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 119adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The {@code FieldPosition} selector for 'y' field alignment, corresponds 120adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * to the {@link Calendar#YEAR} field. 121adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1220d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int YEAR_FIELD = 1; 123adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 124adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 125adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The {@code FieldPosition} selector for 'M' field alignment, corresponds 126adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * to the {@link Calendar#MONTH} field. 127adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1280d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int MONTH_FIELD = 2; 129adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 130adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 131adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The {@code FieldPosition} selector for 'd' field alignment, corresponds 132adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * to the {@link Calendar#DATE} field. 133adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1340d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int DATE_FIELD = 3; 135adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 136adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 137adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The {@code FieldPosition} selector for 'k' field alignment, corresponds 138adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * to the {@link Calendar#HOUR_OF_DAY} field. {@code HOUR_OF_DAY1_FIELD} is 139adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * used for the one-based 24-hour clock. For example, 23:59 + 01:00 results 140adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * in 24:59. 141adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1420d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int HOUR_OF_DAY1_FIELD = 4; 143adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 144adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 145adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The {@code FieldPosition} selector for 'H' field alignment, corresponds 146adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * to the {@link Calendar#HOUR_OF_DAY} field. {@code HOUR_OF_DAY0_FIELD} is 147adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * used for the zero-based 24-hour clock. For example, 23:59 + 01:00 results 148adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * in 00:59. 149adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1500d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int HOUR_OF_DAY0_FIELD = 5; 151adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 152adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 153adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * FieldPosition selector for 'm' field alignment, corresponds to the 154adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link Calendar#MINUTE} field. 155adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1560d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int MINUTE_FIELD = 6; 157adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 158adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 159adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * FieldPosition selector for 's' field alignment, corresponds to the 160adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link Calendar#SECOND} field. 161adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1620d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int SECOND_FIELD = 7; 163adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 164adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 165adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * FieldPosition selector for 'S' field alignment, corresponds to the 166adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link Calendar#MILLISECOND} field. 167adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1680d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int MILLISECOND_FIELD = 8; 169adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 170adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 171adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * FieldPosition selector for 'E' field alignment, corresponds to the 172adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link Calendar#DAY_OF_WEEK} field. 173adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1740d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int DAY_OF_WEEK_FIELD = 9; 175adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 176adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 177adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * FieldPosition selector for 'D' field alignment, corresponds to the 178adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link Calendar#DAY_OF_YEAR} field. 179adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1800d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int DAY_OF_YEAR_FIELD = 10; 181adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 182adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 183adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * FieldPosition selector for 'F' field alignment, corresponds to the 184adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link Calendar#DAY_OF_WEEK_IN_MONTH} field. 185adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1860d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int DAY_OF_WEEK_IN_MONTH_FIELD = 11; 187adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 188adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 189adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * FieldPosition selector for 'w' field alignment, corresponds to the 190adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link Calendar#WEEK_OF_YEAR} field. 191adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1920d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int WEEK_OF_YEAR_FIELD = 12; 193adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 194adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 195adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * FieldPosition selector for 'W' field alignment, corresponds to the 196adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link Calendar#WEEK_OF_MONTH} field. 197adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 1980d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int WEEK_OF_MONTH_FIELD = 13; 199adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 200adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 201adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * FieldPosition selector for 'a' field alignment, corresponds to the 202adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link Calendar#AM_PM} field. 203adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 2040d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int AM_PM_FIELD = 14; 205adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 206adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 207adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * FieldPosition selector for 'h' field alignment, corresponding to the 208143e8c9cf91cfc01c3c91c8e93cad661ec7554eeElliott Hughes * {@link Calendar#HOUR} field. 209adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 2100d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int HOUR1_FIELD = 15; 211adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 212adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 213143e8c9cf91cfc01c3c91c8e93cad661ec7554eeElliott Hughes * The {@code FieldPosition} selector for 'K' field alignment, corresponding to the 214143e8c9cf91cfc01c3c91c8e93cad661ec7554eeElliott Hughes * {@link Calendar#HOUR} field. 215adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 2160d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int HOUR0_FIELD = 16; 217adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 218adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 219adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The {@code FieldPosition} selector for 'z' field alignment, corresponds 220adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * to the {@link Calendar#ZONE_OFFSET} and {@link Calendar#DST_OFFSET} 221adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * fields. 222adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 2230d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final int TIMEZONE_FIELD = 17; 224adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 225adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 226adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Constructs a new instance of {@code DateFormat}. 227adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 228adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project protected DateFormat() { 229adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 230adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 231adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 232adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a new instance of {@code DateFormat} with the same properties. 233adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 234adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project @Override 235adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public Object clone() { 236adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project DateFormat clone = (DateFormat) super.clone(); 237adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project clone.calendar = (Calendar) calendar.clone(); 238adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project clone.numberFormat = (NumberFormat) numberFormat.clone(); 239adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return clone; 240adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 241adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 242adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 243adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Compares this date format with the specified object and indicates if they 244adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * are equal. 245f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 246adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param object 247adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the object to compare with this date format. 248adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return {@code true} if {@code object} is a {@code DateFormat} object and 249adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * it has the same properties as this date format; {@code false} 250adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * otherwise. 251adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @see #hashCode 252adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 253adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project @Override 254adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public boolean equals(Object object) { 255adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (this == object) { 256adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return true; 257adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 258adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (!(object instanceof DateFormat)) { 259adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return false; 260adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 261adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project DateFormat dateFormat = (DateFormat) object; 262adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return numberFormat.equals(dateFormat.numberFormat) 263adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project && calendar.getTimeZone().equals( 264adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project dateFormat.calendar.getTimeZone()) 265adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project && calendar.getFirstDayOfWeek() == dateFormat.calendar 266adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project .getFirstDayOfWeek() 267adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project && calendar.getMinimalDaysInFirstWeek() == dateFormat.calendar 268adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project .getMinimalDaysInFirstWeek() 269adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project && calendar.isLenient() == dateFormat.calendar.isLenient(); 270adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 271adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 272adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 273adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Formats the specified object as a string using the pattern of this date 274adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * format and appends the string to the specified string buffer. 275adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p> 276adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * If the {@code field} member of {@code field} contains a value specifying 277adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * a format field, then its {@code beginIndex} and {@code endIndex} members 278adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * will be updated with the position of the first occurrence of this field 279adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * in the formatted text. 2809b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * 281adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param object 282adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the source object to format, must be a {@code Date} or a 283adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code Number}. If {@code object} is a number then a date is 284adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * constructed using the {@code longValue()} of the number. 285adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param buffer 286adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the target string buffer to append the formatted date/time to. 287adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param field 288adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * on input: an optional alignment field; on output: the offsets 289adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * of the alignment field in the formatted text. 290adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the string buffer. 2919b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * @throws IllegalArgumentException 2929b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * if {@code object} is neither a {@code Date} nor a 2939b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * {@code Number} instance. 294adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 295adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project @Override 296cff1616012dc0d56c2da9af2b9b1183e76c7e044Elliott Hughes public final StringBuffer format(Object object, StringBuffer buffer, FieldPosition field) { 297adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (object instanceof Date) { 298adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return format((Date) object, buffer, field); 299adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 300adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (object instanceof Number) { 301cff1616012dc0d56c2da9af2b9b1183e76c7e044Elliott Hughes return format(new Date(((Number) object).longValue()), buffer, field); 302adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 303cff1616012dc0d56c2da9af2b9b1183e76c7e044Elliott Hughes throw new IllegalArgumentException("Bad class: " + object.getClass()); 304adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 305adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 306adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 307adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Formats the specified date using the rules of this date format. 308f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 309adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param date 310adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the date to format. 311adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the formatted string. 312adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 313adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public final String format(Date date) { 314cff1616012dc0d56c2da9af2b9b1183e76c7e044Elliott Hughes return format(date, new StringBuffer(), new FieldPosition(0)).toString(); 315adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 316adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 317adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 318adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Formats the specified date as a string using the pattern of this date 319adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * format and appends the string to the specified string buffer. 320adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p> 321adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * If the {@code field} member of {@code field} contains a value specifying 322adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * a format field, then its {@code beginIndex} and {@code endIndex} members 323adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * will be updated with the position of the first occurrence of this field 324adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * in the formatted text. 3259b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * 326adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param date 327adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the date to format. 328adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param buffer 329adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the target string buffer to append the formatted date/time to. 330adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param field 331adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * on input: an optional alignment field; on output: the offsets 332adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * of the alignment field in the formatted text. 333adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the string buffer. 334adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 335cff1616012dc0d56c2da9af2b9b1183e76c7e044Elliott Hughes public abstract StringBuffer format(Date date, StringBuffer buffer, FieldPosition field); 336adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 337adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 33893f0d794f264baeb7a90d02e58cae60305b3912aElliott Hughes * Returns an array of locales for which custom {@code DateFormat} instances 33993f0d794f264baeb7a90d02e58cae60305b3912aElliott Hughes * are available. 340d2d7abef3e9b73a57cdf1f2afd678d7f48945679Elliott Hughes * <p>Note that Android does not support user-supplied locale service providers. 341adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 342adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public static Locale[] getAvailableLocales() { 343757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes return ICU.getAvailableDateFormatLocales(); 344adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 345adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 346adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 347adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns the calendar used by this {@code DateFormat}. 348f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 349adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the calendar used by this date format. 350adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 351adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public Calendar getCalendar() { 352adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return calendar; 353adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 354adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 355adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 356adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a {@code DateFormat} instance for formatting and parsing dates in 357adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the DEFAULT style for the default locale. 358f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 359adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code DateFormat} instance for the default style and locale. 360adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 3610d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final DateFormat getDateInstance() { 362adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return getDateInstance(DEFAULT); 363adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 364adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 365adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 366adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a {@code DateFormat} instance for formatting and parsing dates in 3673106a99ccbe2e2a25bb66266d0ee42d98dd18099Elliott Hughes * the specified style for the user's default locale. 3683106a99ccbe2e2a25bb66266d0ee42d98dd18099Elliott Hughes * See "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>". 369adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param style 370adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. 371adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code DateFormat} instance for {@code style} and the default 372adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * locale. 373adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 374adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code style} is not one of SHORT, MEDIUM, LONG, FULL, or 375adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * DEFAULT. 376adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 3770d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final DateFormat getDateInstance(int style) { 378adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project checkDateStyle(style); 379adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return getDateInstance(style, Locale.getDefault()); 380adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 381adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 382adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 383adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a {@code DateFormat} instance for formatting and parsing dates in 384adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the specified style for the specified locale. 385f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 386adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param style 387adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. 388adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param locale 389adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the locale. 390adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 391adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code style} is not one of SHORT, MEDIUM, LONG, FULL, or 392adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * DEFAULT. 393adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code DateFormat} instance for {@code style} and 394adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code locale}. 395adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 3960d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final DateFormat getDateInstance(int style, Locale locale) { 397adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project checkDateStyle(style); 398e4f01dff49442840faa828dcff0d1583a2e68530Narayan Kamath if (locale == null) { 399e4f01dff49442840faa828dcff0d1583a2e68530Narayan Kamath throw new NullPointerException("locale == null"); 400e4f01dff49442840faa828dcff0d1583a2e68530Narayan Kamath } 401757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes return new SimpleDateFormat(LocaleData.get(locale).getDateFormat(style), locale); 402adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 403adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 404adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 405adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a {@code DateFormat} instance for formatting and parsing dates 406adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * and time values in the DEFAULT style for the default locale. 407f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 408adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code DateFormat} instance for the default style and locale. 409adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 4100d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final DateFormat getDateTimeInstance() { 411adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return getDateTimeInstance(DEFAULT, DEFAULT); 412adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 413adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 414adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 415adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a {@code DateFormat} instance for formatting and parsing of both 4163106a99ccbe2e2a25bb66266d0ee42d98dd18099Elliott Hughes * dates and time values in the manner appropriate for the user's default locale. 4173106a99ccbe2e2a25bb66266d0ee42d98dd18099Elliott Hughes * See "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>". 418adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param dateStyle 419adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. 420adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param timeStyle 421adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. 422adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code DateFormat} instance for {@code dateStyle}, 423adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code timeStyle} and the default locale. 424adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 425adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code dateStyle} or {@code timeStyle} is not one of 426adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * SHORT, MEDIUM, LONG, FULL, or DEFAULT. 427adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 4280d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle) { 429adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project checkTimeStyle(timeStyle); 430adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project checkDateStyle(dateStyle); 431adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return getDateTimeInstance(dateStyle, timeStyle, Locale.getDefault()); 432adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 433adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 434adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 435adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a {@code DateFormat} instance for formatting and parsing dates 436adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * and time values in the specified styles for the specified locale. 437f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 438adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param dateStyle 439adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. 440adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param timeStyle 441adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. 442adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param locale 443adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the locale. 444adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code DateFormat} instance for {@code dateStyle}, 445adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code timeStyle} and {@code locale}. 446adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 447adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code dateStyle} or {@code timeStyle} is not one of 448adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * SHORT, MEDIUM, LONG, FULL, or DEFAULT. 449adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 4500d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale) { 451adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project checkTimeStyle(timeStyle); 452adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project checkDateStyle(dateStyle); 453e4f01dff49442840faa828dcff0d1583a2e68530Narayan Kamath if (locale == null) { 454e4f01dff49442840faa828dcff0d1583a2e68530Narayan Kamath throw new NullPointerException("locale == null"); 455e4f01dff49442840faa828dcff0d1583a2e68530Narayan Kamath } 456757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes LocaleData localeData = LocaleData.get(locale); 4576ca85c49efc0f02d69933f60b207b964a999061fElliott Hughes String pattern = localeData.getDateFormat(dateStyle) + " " + localeData.getTimeFormat(timeStyle); 458adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return new SimpleDateFormat(pattern, locale); 459adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 460adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 461adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 462adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a {@code DateFormat} instance for formatting and parsing dates 463adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * and times in the SHORT style for the default locale. 464f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 465adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code DateFormat} instance for the SHORT style and default 466adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * locale. 467adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 4680d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final DateFormat getInstance() { 469adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return getDateTimeInstance(SHORT, SHORT); 470adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 471adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 472adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 47368d78caff1997dd870f0265c1c642288aee6e653Narayan Kamath * @hide for internal use only. 47468d78caff1997dd870f0265c1c642288aee6e653Narayan Kamath */ 47568d78caff1997dd870f0265c1c642288aee6e653Narayan Kamath public static final void set24HourTimePref(boolean is24Hour) { 4760836eb6118ddedbfe420b5d6ff98f07adf5a5d23Elliott Hughes DateFormat.is24Hour = is24Hour; 47768d78caff1997dd870f0265c1c642288aee6e653Narayan Kamath } 47868d78caff1997dd870f0265c1c642288aee6e653Narayan Kamath 47968d78caff1997dd870f0265c1c642288aee6e653Narayan Kamath /** 480adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns the {@code NumberFormat} used by this {@code DateFormat}. 481f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 482adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code NumberFormat} used by this date format. 483adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 484adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public NumberFormat getNumberFormat() { 485adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return numberFormat; 486adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 487adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 488adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 489adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a {@code DateFormat} instance for formatting and parsing time 490adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * values in the DEFAULT style for the default locale. 491f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 492adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code DateFormat} instance for the default style and locale. 493adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 4940d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final DateFormat getTimeInstance() { 495adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return getTimeInstance(DEFAULT); 496adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 497adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 498adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 499adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a {@code DateFormat} instance for formatting and parsing time 5003106a99ccbe2e2a25bb66266d0ee42d98dd18099Elliott Hughes * values in the specified style for the user's default locale. 5013106a99ccbe2e2a25bb66266d0ee42d98dd18099Elliott Hughes * See "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>". 502adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param style 503adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. 504adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code DateFormat} instance for {@code style} and the default 505adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * locale. 506adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 507adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code style} is not one of SHORT, MEDIUM, LONG, FULL, or 508adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * DEFAULT. 509adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 5100d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final DateFormat getTimeInstance(int style) { 511adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project checkTimeStyle(style); 512adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return getTimeInstance(style, Locale.getDefault()); 513adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 514adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 515adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 516adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a {@code DateFormat} instance for formatting and parsing time 517adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * values in the specified style for the specified locale. 518f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 519adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param style 520adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. 521adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param locale 522adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the locale. 523adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 524adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code style} is not one of SHORT, MEDIUM, LONG, FULL, or 525adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * DEFAULT. 526adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code DateFormat} instance for {@code style} and 527adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code locale}. 528adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 5290d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final DateFormat getTimeInstance(int style, Locale locale) { 530adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project checkTimeStyle(style); 531e4f01dff49442840faa828dcff0d1583a2e68530Narayan Kamath if (locale == null) { 532e4f01dff49442840faa828dcff0d1583a2e68530Narayan Kamath throw new NullPointerException("locale == null"); 533e4f01dff49442840faa828dcff0d1583a2e68530Narayan Kamath } 534e4f01dff49442840faa828dcff0d1583a2e68530Narayan Kamath 535757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes return new SimpleDateFormat(LocaleData.get(locale).getTimeFormat(style), locale); 536adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 537adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 538adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 539adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns the time zone of this date format's calendar. 540f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 541adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the time zone of the calendar used by this date format. 542adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 543adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public TimeZone getTimeZone() { 544adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return calendar.getTimeZone(); 545adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 546adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 547adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project @Override 548adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public int hashCode() { 549adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return calendar.getFirstDayOfWeek() 550adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project + calendar.getMinimalDaysInFirstWeek() 551adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project + calendar.getTimeZone().hashCode() 552adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project + (calendar.isLenient() ? 1231 : 1237) 553adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project + numberFormat.hashCode(); 554adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 555adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 556adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 557adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Indicates whether the calendar used by this date format is lenient. 558f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 559adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return {@code true} if the calendar is lenient; {@code false} otherwise. 560adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 561adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public boolean isLenient() { 562adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return calendar.isLenient(); 563adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 564adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 565adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 566adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Parses a date from the specified string using the rules of this date 567adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * format. 568f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 569adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param string 570adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the string to parse. 571adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code Date} resulting from the parsing. 5729b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * @throws ParseException 5739b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * if an error occurs during parsing. 574adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 575adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public Date parse(String string) throws ParseException { 576adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project ParsePosition position = new ParsePosition(0); 577adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project Date date = parse(string, position); 578fe1eba10c5d69e115dfab55d82cc22d42e9fbae5Elliott Hughes if (position.getIndex() == 0) { 579a8bc3bd2794b36c5e935aff7b932a7945e98bf30Elliott Hughes throw new ParseException("Unparseable date: \"" + string + "\"", 580a8bc3bd2794b36c5e935aff7b932a7945e98bf30Elliott Hughes position.getErrorIndex()); 581adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 582adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return date; 583adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 584adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 585adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 586adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Parses a date from the specified string starting at the index specified 587adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * by {@code position}. If the string is successfully parsed then the index 588adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * of the {@code ParsePosition} is updated to the index following the parsed 589adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * text. On error, the index is unchanged and the error index of {@code 590adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * ParsePosition} is set to the index where the error occurred. 591adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p> 592adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * By default, parsing is lenient: If the input is not in the form used by 593adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * this object's format method but can still be parsed as a date, then the 594adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * parse succeeds. Clients may insist on strict adherence to the format by 595adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * calling {@code setLenient(false)}. 5969b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * 597adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param string 598adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the string to parse. 599adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param position 600adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * input/output parameter, specifies the start index in {@code 601adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * string} from where to start parsing. If parsing is successful, 602adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * it is updated with the index following the parsed text; on 603adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * error, the index is unchanged and the error index is set to 604adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the index where the error occurred. 605adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the date resulting from the parse, or {@code null} if there is an 606adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * error. 607adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 608adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public abstract Date parse(String string, ParsePosition position); 609adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 610adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 611adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Parses a date from the specified string starting at the index specified 612adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * by {@code position}. If the string is successfully parsed then the index 613adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * of the {@code ParsePosition} is updated to the index following the parsed 614adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * text. On error, the index is unchanged and the error index of 615adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code ParsePosition} is set to the index where the error occurred. 616adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p> 617adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * By default, parsing is lenient: If the input is not in the form used by 618adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * this object's format method but can still be parsed as a date, then the 619adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * parse succeeds. Clients may insist on strict adherence to the format by 620adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * calling {@code setLenient(false)}. 6219b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * 622adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param string 623adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the string to parse. 624adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param position 625adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * input/output parameter, specifies the start index in 626adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code string} from where to start parsing. If parsing is 627adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * successful, it is updated with the index following the parsed 628adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * text; on error, the index is unchanged and the error index 629adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * is set to the index where the error occurred. 630adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the date resulting from the parsing, or {@code null} if there is 631adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * an error. 632adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 633adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project @Override 634adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public Object parseObject(String string, ParsePosition position) { 635adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return parse(string, position); 636adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 637adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 638adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 639adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Sets the calendar used by this date format. 640f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 641adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param cal 642adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the new calendar. 643adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 644adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public void setCalendar(Calendar cal) { 645adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project calendar = cal; 646adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 647adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 648adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 649adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Specifies whether or not date/time parsing shall be lenient. With lenient 650adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * parsing, the parser may use heuristics to interpret inputs that do not 651adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * precisely match this object's format. With strict parsing, inputs must 652adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * match this object's format. 653f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 654adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param value 655adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code true} to set the calendar to be lenient, {@code false} 656adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * otherwise. 657adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 658adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public void setLenient(boolean value) { 659adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project calendar.setLenient(value); 660adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 661adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 662adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 663adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Sets the {@code NumberFormat} used by this date format. 664f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 665adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param format 666adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the new number format. 667adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 668adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public void setNumberFormat(NumberFormat format) { 669adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project numberFormat = format; 670adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 671adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 672adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 673adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Sets the time zone of the calendar used by this date format. 674f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 675adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param timezone 676adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the new time zone. 677adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 678adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public void setTimeZone(TimeZone timezone) { 679adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project calendar.setTimeZone(timezone); 680adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 681adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 682adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 683adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The instances of this inner class are used as attribute keys and values 684adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * in {@code AttributedCharacterIterator} that the 685adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link SimpleDateFormat#formatToCharacterIterator(Object)} method returns. 686adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p> 687adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * There is no public constructor in this class, the only instances are the 688adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * constants defined here. 689adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 690adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public static class Field extends Format.Field { 691adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 692adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project private static final long serialVersionUID = 7441350119349544720L; 693adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 694adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project private static Hashtable<Integer, Field> table = new Hashtable<Integer, Field>(); 695adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 696adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 697adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the era part of a date. 698adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 6990d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field ERA = new Field("era", Calendar.ERA); 700adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 701adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 702adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the year part of a date. 703adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7040d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field YEAR = new Field("year", Calendar.YEAR); 705adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 706adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 707adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the month part of a date. 708adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7090d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field MONTH = new Field("month", Calendar.MONTH); 710adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 711adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 712adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the hour of the day part of a date (0-11). 713adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7140d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field HOUR_OF_DAY0 = new Field("hour of day", Calendar.HOUR_OF_DAY); 715adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 716adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 717adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the hour of the day part of a date (1-12). 718adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7190d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field HOUR_OF_DAY1 = new Field("hour of day 1", -1); 720adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 721adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 722adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the minute part of a time. 723adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7240d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field MINUTE = new Field("minute", Calendar.MINUTE); 725adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 726adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 727adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the second part of a time. 728adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7290d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field SECOND = new Field("second", Calendar.SECOND); 730adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 731adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 732adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the millisecond part of a time. 733adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7340d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field MILLISECOND = new Field("millisecond", Calendar.MILLISECOND); 735adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 736adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 737adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the day of the week part of a date. 738adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7390d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field DAY_OF_WEEK = new Field("day of week", Calendar.DAY_OF_WEEK); 740adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 741adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 742adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the day of the month part of a date. 743adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7440d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field DAY_OF_MONTH = new Field("day of month", Calendar.DAY_OF_MONTH); 745adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 746adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 747adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the day of the year part of a date. 748adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7490d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field DAY_OF_YEAR = new Field("day of year", Calendar.DAY_OF_YEAR); 750adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 751adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 752adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the day of the week in the month part of a date. 753adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7540d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field DAY_OF_WEEK_IN_MONTH = new Field("day of week in month", 75503c0a8e681c776fdba0389ab8593282139afc6d6Elliott Hughes Calendar.DAY_OF_WEEK_IN_MONTH); 756adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 757adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 758adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the week of the year part of a date. 759adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7600d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field WEEK_OF_YEAR = new Field("week of year", 761adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project Calendar.WEEK_OF_YEAR); 762adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 763adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 764adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the week of the month part of a date. 765adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7660d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field WEEK_OF_MONTH = new Field("week of month", 767adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project Calendar.WEEK_OF_MONTH); 768adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 769adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 770adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the time indicator part of a date. 771adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7720d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field AM_PM = new Field("am pm", Calendar.AM_PM); 773adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 774adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 775adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the hour part of a date (0-11). 776adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7770d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field HOUR0 = new Field("hour", Calendar.HOUR); 778adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 779adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 780adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the hour part of a date (1-12). 781adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 7820d4ce4227fa818288b8db762b640dfa21e3162f5Elliott Hughes public static final Field HOUR1 = new Field("hour 1", -1); 783adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 784adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 785adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Marks the time zone part of a date. 786adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 78778b660e21412ff36d3eed179ff400d8fa690267eNarayan Kamath public static final Field TIME_ZONE = new Field("time zone", -1); 788adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 789adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 790adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The calendar field that this field represents. 791adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 792adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project private int calendarField = -1; 793adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 794adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 795adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Constructs a new instance of {@code DateFormat.Field} with the given 796adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * fieldName and calendar field. 7979b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson * 798adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param fieldName 799adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the field name. 800adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param calendarField 801adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the calendar field type of the field. 802adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 803adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project protected Field(String fieldName, int calendarField) { 804adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project super(fieldName); 805adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project this.calendarField = calendarField; 806a0a4196cb15480959f053d0ebe6b412bd23c8170Elliott Hughes if (calendarField != -1 && table.get(Integer.valueOf(calendarField)) == null) { 807a0a4196cb15480959f053d0ebe6b412bd23c8170Elliott Hughes table.put(Integer.valueOf(calendarField), this); 808adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 809adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 810adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 811adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 812adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns the Calendar field that this field represents. 813f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 814adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the calendar field. 815adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 816adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public int getCalendarField() { 817adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return calendarField; 818adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 819adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 820adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 821adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns the {@code DateFormat.Field} instance for the given calendar 822adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * field. 823f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 824adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param calendarField 825adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * a calendar field constant. 826adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the {@code DateFormat.Field} corresponding to 827adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code calendarField}. 828adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 829adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code calendarField} is negative or greater than the 830adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * field count of {@code Calendar}. 831adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 832adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public static Field ofCalendarField(int calendarField) { 833adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (calendarField < 0 || calendarField >= Calendar.FIELD_COUNT) { 834cff1616012dc0d56c2da9af2b9b1183e76c7e044Elliott Hughes throw new IllegalArgumentException("Field out of range: " + calendarField); 835adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 836a0a4196cb15480959f053d0ebe6b412bd23c8170Elliott Hughes return table.get(Integer.valueOf(calendarField)); 837adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 838adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 839adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 840adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project private static void checkDateStyle(int style) { 841adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (!(style == SHORT || style == MEDIUM || style == LONG 842adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project || style == FULL || style == DEFAULT)) { 843cff1616012dc0d56c2da9af2b9b1183e76c7e044Elliott Hughes throw new IllegalArgumentException("Illegal date style: " + style); 844adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 845adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 846adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 847adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project private static void checkTimeStyle(int style) { 848adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (!(style == SHORT || style == MEDIUM || style == LONG 849adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project || style == FULL || style == DEFAULT)) { 850cff1616012dc0d56c2da9af2b9b1183e76c7e044Elliott Hughes throw new IllegalArgumentException("Illegal time style: " + style); 851adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 852adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 853adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project} 854