281 * Useful constant for one-based HOUR field alignment.
282 * Used in FieldPosition of date/time formatting.
283 * HOUR1_FIELD is used for the one-based 12-hour clock.
284 * For example, 11:30 PM + 1 hour results in 12:30 AM.
285 */
286 public static final int HOUR1_FIELD = 15;
287 /**
288 * Useful constant for zero-based HOUR field alignment.
289 * Used in FieldPosition of date/time formatting.
290 * HOUR0_FIELD is used for the zero-based 12-hour clock.
291 * For example, 11:30 PM + 1 hour results in 00:30 AM.
292 */
293 public static final int HOUR0_FIELD = 16;
294 /**
295 * Useful constant for TIMEZONE field alignment.
296 * Used in FieldPosition of date/time formatting.
297 */
298 public static final int TIMEZONE_FIELD = 17;
299
300 // Proclaim serial compatibility with 1.1 FCS
301 private static final long serialVersionUID = 7218322306649953788L;
302
303 /**
304 * Formats the given {@code Object} into a date-time string. The formatted
305 * string is appended to the given {@code StringBuffer}.
306 *
307 * @param obj Must be a {@code Date} or a {@code Number} representing a
308 * millisecond offset from the <a href="../util/Calendar.html#Epoch">Epoch</a>.
309 * @param toAppendTo The string buffer for the returning date-time string.
310 * @param fieldPosition keeps track on the position of the field within
311 * the returned string. For example, given a date-time text
312 * {@code "1996.07.10 AD at 15:08:56 PDT"}, if the given {@code fieldPosition}
313 * is {@link DateFormat#YEAR_FIELD}, the begin index and end index of
314 * {@code fieldPosition} will be set to 0 and 4, respectively.
315 * Notice that if the same date-time field appears more than once in a
316 * pattern, the {@code fieldPosition} will be set for the first occurrence
317 * of that date-time field. For instance, formatting a {@code Date} to the
318 * date-time string {@code "1 PM PDT (Pacific Daylight Time)"} using the
319 * pattern {@code "h a z (zzzz)"} and the alignment field
320 * {@link DateFormat#TIMEZONE_FIELD}, the begin index and end index of
849 /**
850 * Create a new date format.
851 */
852 protected DateFormat() {}
853
854 /**
855 * Defines constants that are used as attribute keys in the
856 * <code>AttributedCharacterIterator</code> returned
857 * from <code>DateFormat.formatToCharacterIterator</code> and as
858 * field identifiers in <code>FieldPosition</code>.
859 * <p>
860 * The class also provides two methods to map
861 * between its constants and the corresponding Calendar constants.
862 *
863 * @since 1.4
864 * @see java.util.Calendar
865 */
866 public static class Field extends Format.Field {
867
868 // Proclaim serial compatibility with 1.4 FCS
869 private static final long serialVersionUID = 7441350119349544720L;
870
871 // table of all instances in this class, used by readResolve
872 private static final Map<String, Field> instanceMap = new HashMap<>(18);
873 // Maps from Calendar constant (such as Calendar.ERA) to Field
874 // constant (such as Field.ERA).
875 private static final Field[] calendarToFieldMapping =
876 new Field[Calendar.FIELD_COUNT];
877
878 /** Calendar field. */
879 private int calendarField;
880
881 /**
882 * Returns the <code>Field</code> constant that corresponds to
883 * the <code>Calendar</code> constant <code>calendarField</code>.
884 * If there is no direct mapping between the <code>Calendar</code>
885 * constant and a <code>Field</code>, null is returned.
886 *
887 * @throws IllegalArgumentException if <code>calendarField</code> is
888 * not the value of a <code>Calendar</code> field constant.
926 * attribute. For example, if this represents the hours field of
927 * a <code>Calendar</code>, this would return
928 * <code>Calendar.HOUR</code>. If there is no corresponding
929 * <code>Calendar</code> constant, this will return -1.
930 *
931 * @return Calendar constant for this field
932 * @see java.util.Calendar
933 */
934 public int getCalendarField() {
935 return calendarField;
936 }
937
938 /**
939 * Resolves instances being deserialized to the predefined constants.
940 *
941 * @throws InvalidObjectException if the constant could not be
942 * resolved.
943 * @return resolved DateFormat.Field constant
944 */
945 @Override
946 protected Object readResolve() throws InvalidObjectException {
947 if (this.getClass() != DateFormat.Field.class) {
948 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
949 }
950
951 Object instance = instanceMap.get(getName());
952 if (instance != null) {
953 return instance;
954 } else {
955 throw new InvalidObjectException("unknown attribute name");
956 }
957 }
958
959 //
960 // The constants
961 //
962
963 /**
964 * Constant identifying the era field.
965 */
|
281 * Useful constant for one-based HOUR field alignment.
282 * Used in FieldPosition of date/time formatting.
283 * HOUR1_FIELD is used for the one-based 12-hour clock.
284 * For example, 11:30 PM + 1 hour results in 12:30 AM.
285 */
286 public static final int HOUR1_FIELD = 15;
287 /**
288 * Useful constant for zero-based HOUR field alignment.
289 * Used in FieldPosition of date/time formatting.
290 * HOUR0_FIELD is used for the zero-based 12-hour clock.
291 * For example, 11:30 PM + 1 hour results in 00:30 AM.
292 */
293 public static final int HOUR0_FIELD = 16;
294 /**
295 * Useful constant for TIMEZONE field alignment.
296 * Used in FieldPosition of date/time formatting.
297 */
298 public static final int TIMEZONE_FIELD = 17;
299
300 // Proclaim serial compatibility with 1.1 FCS
301 @java.io.Serial
302 private static final long serialVersionUID = 7218322306649953788L;
303
304 /**
305 * Formats the given {@code Object} into a date-time string. The formatted
306 * string is appended to the given {@code StringBuffer}.
307 *
308 * @param obj Must be a {@code Date} or a {@code Number} representing a
309 * millisecond offset from the <a href="../util/Calendar.html#Epoch">Epoch</a>.
310 * @param toAppendTo The string buffer for the returning date-time string.
311 * @param fieldPosition keeps track on the position of the field within
312 * the returned string. For example, given a date-time text
313 * {@code "1996.07.10 AD at 15:08:56 PDT"}, if the given {@code fieldPosition}
314 * is {@link DateFormat#YEAR_FIELD}, the begin index and end index of
315 * {@code fieldPosition} will be set to 0 and 4, respectively.
316 * Notice that if the same date-time field appears more than once in a
317 * pattern, the {@code fieldPosition} will be set for the first occurrence
318 * of that date-time field. For instance, formatting a {@code Date} to the
319 * date-time string {@code "1 PM PDT (Pacific Daylight Time)"} using the
320 * pattern {@code "h a z (zzzz)"} and the alignment field
321 * {@link DateFormat#TIMEZONE_FIELD}, the begin index and end index of
850 /**
851 * Create a new date format.
852 */
853 protected DateFormat() {}
854
855 /**
856 * Defines constants that are used as attribute keys in the
857 * <code>AttributedCharacterIterator</code> returned
858 * from <code>DateFormat.formatToCharacterIterator</code> and as
859 * field identifiers in <code>FieldPosition</code>.
860 * <p>
861 * The class also provides two methods to map
862 * between its constants and the corresponding Calendar constants.
863 *
864 * @since 1.4
865 * @see java.util.Calendar
866 */
867 public static class Field extends Format.Field {
868
869 // Proclaim serial compatibility with 1.4 FCS
870 @java.io.Serial
871 private static final long serialVersionUID = 7441350119349544720L;
872
873 // table of all instances in this class, used by readResolve
874 private static final Map<String, Field> instanceMap = new HashMap<>(18);
875 // Maps from Calendar constant (such as Calendar.ERA) to Field
876 // constant (such as Field.ERA).
877 private static final Field[] calendarToFieldMapping =
878 new Field[Calendar.FIELD_COUNT];
879
880 /** Calendar field. */
881 private int calendarField;
882
883 /**
884 * Returns the <code>Field</code> constant that corresponds to
885 * the <code>Calendar</code> constant <code>calendarField</code>.
886 * If there is no direct mapping between the <code>Calendar</code>
887 * constant and a <code>Field</code>, null is returned.
888 *
889 * @throws IllegalArgumentException if <code>calendarField</code> is
890 * not the value of a <code>Calendar</code> field constant.
928 * attribute. For example, if this represents the hours field of
929 * a <code>Calendar</code>, this would return
930 * <code>Calendar.HOUR</code>. If there is no corresponding
931 * <code>Calendar</code> constant, this will return -1.
932 *
933 * @return Calendar constant for this field
934 * @see java.util.Calendar
935 */
936 public int getCalendarField() {
937 return calendarField;
938 }
939
940 /**
941 * Resolves instances being deserialized to the predefined constants.
942 *
943 * @throws InvalidObjectException if the constant could not be
944 * resolved.
945 * @return resolved DateFormat.Field constant
946 */
947 @Override
948 @java.io.Serial
949 protected Object readResolve() throws InvalidObjectException {
950 if (this.getClass() != DateFormat.Field.class) {
951 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
952 }
953
954 Object instance = instanceMap.get(getName());
955 if (instance != null) {
956 return instance;
957 } else {
958 throw new InvalidObjectException("unknown attribute name");
959 }
960 }
961
962 //
963 // The constants
964 //
965
966 /**
967 * Constant identifying the era field.
968 */
|