33 * and Sun. This technology is protected by multiple US and International
34 * patents. This notice and attribution to Taligent may not be removed.
35 * Taligent is a registered trademark of Taligent, Inc.
36 *
37 */
38
39 package java.text;
40
41 import java.io.InvalidObjectException;
42 import java.text.spi.DateFormatProvider;
43 import java.util.Calendar;
44 import java.util.Date;
45 import java.util.GregorianCalendar;
46 import java.util.HashMap;
47 import java.util.Locale;
48 import java.util.Map;
49 import java.util.MissingResourceException;
50 import java.util.ResourceBundle;
51 import java.util.TimeZone;
52 import java.util.spi.LocaleServiceProvider;
53 import sun.util.LocaleServiceProviderPool;
54
55 /**
56 * {@code DateFormat} is an abstract class for date/time formatting subclasses which
57 * formats and parses dates or time in a language-independent manner.
58 * The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for
59 * formatting (i.e., date -> text), parsing (text -> date), and
60 * normalization. The date is represented as a <code>Date</code> object or
61 * as the milliseconds since January 1, 1970, 00:00:00 GMT.
62 *
63 * <p>{@code DateFormat} provides many class methods for obtaining default date/time
64 * formatters based on the default or a given locale and a number of formatting
65 * styles. The formatting styles include {@link #FULL}, {@link #LONG}, {@link #MEDIUM}, and {@link #SHORT}. More
66 * detail and examples of using these styles are provided in the method
67 * descriptions.
68 *
69 * <p>{@code DateFormat} helps you to format and parse dates for any locale.
70 * Your code can be completely independent of the locale conventions for
71 * months, days of the week, or even the calendar format: lunar vs. solar.
72 *
73 * <p>To format a date for the current Locale, use one of the
735 * @param flags either 1 for a time format, 2 for a date format,
736 * or 3 for a date/time format
737 * @param loc the locale for the format
738 */
739 private static DateFormat get(int timeStyle, int dateStyle,
740 int flags, Locale loc) {
741 if ((flags & 1) != 0) {
742 if (timeStyle < 0 || timeStyle > 3) {
743 throw new IllegalArgumentException("Illegal time style " + timeStyle);
744 }
745 } else {
746 timeStyle = -1;
747 }
748 if ((flags & 2) != 0) {
749 if (dateStyle < 0 || dateStyle > 3) {
750 throw new IllegalArgumentException("Illegal date style " + dateStyle);
751 }
752 } else {
753 dateStyle = -1;
754 }
755 try {
756 // Check whether a provider can provide an implementation that's closer
757 // to the requested locale than what the Java runtime itself can provide.
758 LocaleServiceProviderPool pool =
759 LocaleServiceProviderPool.getPool(DateFormatProvider.class);
760 if (pool.hasProviders()) {
761 DateFormat providersInstance = pool.getLocalizedObject(
762 DateFormatGetter.INSTANCE,
763 loc,
764 timeStyle,
765 dateStyle,
766 flags);
767 if (providersInstance != null) {
768 return providersInstance;
769 }
770 }
771
772 return new SimpleDateFormat(timeStyle, dateStyle, loc);
773 } catch (MissingResourceException e) {
774 return new SimpleDateFormat("M/d/yy h:mm a");
775 }
776 }
777
778 /**
779 * Create a new date format.
780 */
781 protected DateFormat() {}
782
783 /**
784 * Defines constants that are used as attribute keys in the
785 * <code>AttributedCharacterIterator</code> returned
786 * from <code>DateFormat.formatToCharacterIterator</code> and as
787 * field identifiers in <code>FieldPosition</code>.
788 * <p>
789 * The class also provides two methods to map
790 * between its constants and the corresponding Calendar constants.
791 *
792 * @since 1.4
793 * @see java.util.Calendar
794 */
795 public static class Field extends Format.Field {
796
854 * Returns the <code>Calendar</code> field associated with this
855 * attribute. For example, if this represents the hours field of
856 * a <code>Calendar</code>, this would return
857 * <code>Calendar.HOUR</code>. If there is no corresponding
858 * <code>Calendar</code> constant, this will return -1.
859 *
860 * @return Calendar constant for this field
861 * @see java.util.Calendar
862 */
863 public int getCalendarField() {
864 return calendarField;
865 }
866
867 /**
868 * Resolves instances being deserialized to the predefined constants.
869 *
870 * @throws InvalidObjectException if the constant could not be
871 * resolved.
872 * @return resolved DateFormat.Field constant
873 */
874 protected Object readResolve() throws InvalidObjectException {
875 if (this.getClass() != DateFormat.Field.class) {
876 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
877 }
878
879 Object instance = instanceMap.get(getName());
880 if (instance != null) {
881 return instance;
882 } else {
883 throw new InvalidObjectException("unknown attribute name");
884 }
885 }
886
887 //
888 // The constants
889 //
890
891 /**
892 * Constant identifying the era field.
893 */
977 Field("am pm", Calendar.AM_PM);
978
979 /**
980 * Constant identifying the hour field, where the legal values are
981 * 1 to 12.
982 */
983 public final static Field HOUR1 = new Field("hour 1", -1);
984
985 /**
986 * Constant identifying the hour field, where the legal values are
987 * 0 to 11.
988 */
989 public final static Field HOUR0 = new
990 Field("hour", Calendar.HOUR);
991
992 /**
993 * Constant identifying the time zone field.
994 */
995 public final static Field TIME_ZONE = new Field("time zone", -1);
996 }
997
998 /**
999 * Obtains a DateFormat instance from a DateFormatProvider
1000 * implementation.
1001 */
1002 private static class DateFormatGetter
1003 implements LocaleServiceProviderPool.LocalizedObjectGetter<DateFormatProvider, DateFormat> {
1004 private static final DateFormatGetter INSTANCE = new DateFormatGetter();
1005
1006 public DateFormat getObject(DateFormatProvider dateFormatProvider,
1007 Locale locale,
1008 String key,
1009 Object... params) {
1010 assert params.length == 3;
1011
1012 int timeStyle = (Integer)params[0];
1013 int dateStyle = (Integer)params[1];
1014 int flags = (Integer)params[2];
1015
1016 switch (flags) {
1017 case 1:
1018 return dateFormatProvider.getTimeInstance(timeStyle, locale);
1019 case 2:
1020 return dateFormatProvider.getDateInstance(dateStyle, locale);
1021 case 3:
1022 return dateFormatProvider.getDateTimeInstance(dateStyle, timeStyle, locale);
1023 default:
1024 assert false : "should not happen";
1025 }
1026
1027 return null;
1028 }
1029 }
1030 }
|
33 * and Sun. This technology is protected by multiple US and International
34 * patents. This notice and attribution to Taligent may not be removed.
35 * Taligent is a registered trademark of Taligent, Inc.
36 *
37 */
38
39 package java.text;
40
41 import java.io.InvalidObjectException;
42 import java.text.spi.DateFormatProvider;
43 import java.util.Calendar;
44 import java.util.Date;
45 import java.util.GregorianCalendar;
46 import java.util.HashMap;
47 import java.util.Locale;
48 import java.util.Map;
49 import java.util.MissingResourceException;
50 import java.util.ResourceBundle;
51 import java.util.TimeZone;
52 import java.util.spi.LocaleServiceProvider;
53 import sun.util.locale.provider.LocaleProviderAdapter;
54 import sun.util.locale.provider.LocaleServiceProviderPool;
55
56 /**
57 * {@code DateFormat} is an abstract class for date/time formatting subclasses which
58 * formats and parses dates or time in a language-independent manner.
59 * The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for
60 * formatting (i.e., date -> text), parsing (text -> date), and
61 * normalization. The date is represented as a <code>Date</code> object or
62 * as the milliseconds since January 1, 1970, 00:00:00 GMT.
63 *
64 * <p>{@code DateFormat} provides many class methods for obtaining default date/time
65 * formatters based on the default or a given locale and a number of formatting
66 * styles. The formatting styles include {@link #FULL}, {@link #LONG}, {@link #MEDIUM}, and {@link #SHORT}. More
67 * detail and examples of using these styles are provided in the method
68 * descriptions.
69 *
70 * <p>{@code DateFormat} helps you to format and parse dates for any locale.
71 * Your code can be completely independent of the locale conventions for
72 * months, days of the week, or even the calendar format: lunar vs. solar.
73 *
74 * <p>To format a date for the current Locale, use one of the
736 * @param flags either 1 for a time format, 2 for a date format,
737 * or 3 for a date/time format
738 * @param loc the locale for the format
739 */
740 private static DateFormat get(int timeStyle, int dateStyle,
741 int flags, Locale loc) {
742 if ((flags & 1) != 0) {
743 if (timeStyle < 0 || timeStyle > 3) {
744 throw new IllegalArgumentException("Illegal time style " + timeStyle);
745 }
746 } else {
747 timeStyle = -1;
748 }
749 if ((flags & 2) != 0) {
750 if (dateStyle < 0 || dateStyle > 3) {
751 throw new IllegalArgumentException("Illegal date style " + dateStyle);
752 }
753 } else {
754 dateStyle = -1;
755 }
756
757 LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, loc);
758 DateFormat dateFormat = get(adapter, timeStyle, dateStyle, loc);
759 if (dateFormat == null) {
760 dateFormat = get(LocaleProviderAdapter.forJRE(), timeStyle, dateStyle, loc);
761 }
762 return dateFormat;
763 }
764
765 private static DateFormat get(LocaleProviderAdapter adapter, int timeStyle, int dateStyle, Locale loc) {
766 DateFormatProvider provider = adapter.getDateFormatProvider();
767 DateFormat dateFormat;
768 if (timeStyle == -1) {
769 dateFormat = provider.getDateInstance(dateStyle, loc);
770 } else {
771 if (dateStyle == -1) {
772 dateFormat = provider.getTimeInstance(timeStyle, loc);
773 } else {
774 dateFormat = provider.getDateTimeInstance(dateStyle, timeStyle, loc);
775 }
776 }
777 return dateFormat;
778 }
779
780 /**
781 * Create a new date format.
782 */
783 protected DateFormat() {}
784
785 /**
786 * Defines constants that are used as attribute keys in the
787 * <code>AttributedCharacterIterator</code> returned
788 * from <code>DateFormat.formatToCharacterIterator</code> and as
789 * field identifiers in <code>FieldPosition</code>.
790 * <p>
791 * The class also provides two methods to map
792 * between its constants and the corresponding Calendar constants.
793 *
794 * @since 1.4
795 * @see java.util.Calendar
796 */
797 public static class Field extends Format.Field {
798
856 * Returns the <code>Calendar</code> field associated with this
857 * attribute. For example, if this represents the hours field of
858 * a <code>Calendar</code>, this would return
859 * <code>Calendar.HOUR</code>. If there is no corresponding
860 * <code>Calendar</code> constant, this will return -1.
861 *
862 * @return Calendar constant for this field
863 * @see java.util.Calendar
864 */
865 public int getCalendarField() {
866 return calendarField;
867 }
868
869 /**
870 * Resolves instances being deserialized to the predefined constants.
871 *
872 * @throws InvalidObjectException if the constant could not be
873 * resolved.
874 * @return resolved DateFormat.Field constant
875 */
876 @Override
877 protected Object readResolve() throws InvalidObjectException {
878 if (this.getClass() != DateFormat.Field.class) {
879 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
880 }
881
882 Object instance = instanceMap.get(getName());
883 if (instance != null) {
884 return instance;
885 } else {
886 throw new InvalidObjectException("unknown attribute name");
887 }
888 }
889
890 //
891 // The constants
892 //
893
894 /**
895 * Constant identifying the era field.
896 */
980 Field("am pm", Calendar.AM_PM);
981
982 /**
983 * Constant identifying the hour field, where the legal values are
984 * 1 to 12.
985 */
986 public final static Field HOUR1 = new Field("hour 1", -1);
987
988 /**
989 * Constant identifying the hour field, where the legal values are
990 * 0 to 11.
991 */
992 public final static Field HOUR0 = new
993 Field("hour", Calendar.HOUR);
994
995 /**
996 * Constant identifying the time zone field.
997 */
998 public final static Field TIME_ZONE = new Field("time zone", -1);
999 }
1000 }
|