jdk/src/share/classes/java/time/temporal/ChronoField.java
changeset 16852 60207b2b4b42
parent 15658 55b829ca2334
child 17474 8c100beabcc0
--- a/jdk/src/share/classes/java/time/temporal/ChronoField.java	Thu Apr 11 19:15:24 2013 -0700
+++ b/jdk/src/share/classes/java/time/temporal/ChronoField.java	Fri Apr 12 07:57:35 2013 -0700
@@ -76,6 +76,11 @@
 import java.time.ZoneOffset;
 import java.time.chrono.ChronoLocalDate;
 import java.time.chrono.Chronology;
+import java.util.Locale;
+import java.util.Objects;
+import java.util.ResourceBundle;
+import sun.util.locale.provider.LocaleProviderAdapter;
+import sun.util.locale.provider.LocaleResources;
 
 /**
  * A standard set of fields.
@@ -187,7 +192,7 @@
      * This counts the second within the minute, from 0 to 59.
      * This field has the same meaning for all calendar systems.
      */
-    SECOND_OF_MINUTE("SecondOfMinute", SECONDS, MINUTES, ValueRange.of(0, 59)),
+    SECOND_OF_MINUTE("SecondOfMinute", SECONDS, MINUTES, ValueRange.of(0, 59), "second"),
     /**
      * The second-of-day.
      * <p>
@@ -201,7 +206,7 @@
      * This counts the minute within the hour, from 0 to 59.
      * This field has the same meaning for all calendar systems.
      */
-    MINUTE_OF_HOUR("MinuteOfHour", MINUTES, HOURS, ValueRange.of(0, 59)),
+    MINUTE_OF_HOUR("MinuteOfHour", MINUTES, HOURS, ValueRange.of(0, 59), "minute"),
     /**
      * The minute-of-day.
      * <p>
@@ -232,7 +237,7 @@
      * This is the hour that would be observed on a standard 24-hour digital clock.
      * This field has the same meaning for all calendar systems.
      */
-    HOUR_OF_DAY("HourOfDay", HOURS, DAYS, ValueRange.of(0, 23)),
+    HOUR_OF_DAY("HourOfDay", HOURS, DAYS, ValueRange.of(0, 23), "hour"),
     /**
      * The clock-hour-of-day.
      * <p>
@@ -247,7 +252,7 @@
      * This counts the AM/PM within the day, from 0 (AM) to 1 (PM).
      * This field has the same meaning for all calendar systems.
      */
-    AMPM_OF_DAY("AmPmOfDay", HALF_DAYS, DAYS, ValueRange.of(0, 1)),
+    AMPM_OF_DAY("AmPmOfDay", HALF_DAYS, DAYS, ValueRange.of(0, 1), "dayperiod"),
     /**
      * The day-of-week, such as Tuesday.
      * <p>
@@ -263,7 +268,7 @@
      * if they have a similar concept of named or numbered days within a period similar
      * to a week. It is recommended that the numbering starts from 1.
      */
-    DAY_OF_WEEK("DayOfWeek", DAYS, WEEKS, ValueRange.of(1, 7)),
+    DAY_OF_WEEK("DayOfWeek", DAYS, WEEKS, ValueRange.of(1, 7), "weekday"),
     /**
      * The aligned day-of-week within a month.
      * <p>
@@ -312,7 +317,7 @@
      * day-of-month values for users of the calendar system.
      * Normally, this is a count of days from 1 to the length of the month.
      */
-    DAY_OF_MONTH("DayOfMonth", DAYS, MONTHS, ValueRange.of(1, 28, 31)),
+    DAY_OF_MONTH("DayOfMonth", DAYS, MONTHS, ValueRange.of(1, 28, 31), "day"),
     /**
      * The day-of-year.
      * <p>
@@ -377,17 +382,27 @@
      * month-of-year values for users of the calendar system.
      * Normally, this is a count of months starting from 1.
      */
-    MONTH_OF_YEAR("MonthOfYear", MONTHS, YEARS, ValueRange.of(1, 12)),
+    MONTH_OF_YEAR("MonthOfYear", MONTHS, YEARS, ValueRange.of(1, 12), "month"),
     /**
-     * The epoch-month based on the Java epoch of 1970-01-01.
+     * The proleptic-month based, counting months sequentially from year 0.
      * <p>
-     * This field is the sequential count of months where January 1970 (ISO) is zero.
+     * This field is the sequential count of months where the first month
+     * in proleptic-year zero has the value zero.
+     * Later months have increasingly larger values.
+     * Earlier months have increasingly small values.
+     * There are no gaps or breaks in the sequence of months.
      * Note that this uses the <i>local</i> time-line, ignoring offset and time-zone.
      * <p>
-     * Non-ISO calendar systems should also implement this field to represent a sequential
-     * count of months. It is recommended to define zero as the month of 1970-01-01 (ISO).
+     * In the default ISO calendar system, June 2012 would have the value
+     * {@code (2012 * 12 + 6 - 1)}. This field is primarily for internal use.
+     * <p>
+     * Non-ISO calendar systems must implement this field as per the definition above.
+     * It is just a simple zero-based count of elapsed months from the start of proleptic-year 0.
+     * All calendar systems with a full proleptic-year definition will have a year zero.
+     * If the calendar system has a minimum year that excludes year zero, then one must
+     * be extrapolated in order for this method to be defined.
      */
-    EPOCH_MONTH("EpochMonth", MONTHS, FOREVER, ValueRange.of((Year.MIN_VALUE - 1970L) * 12, (Year.MAX_VALUE - 1970L) * 12L - 1L)),
+    PROLEPTIC_MONTH("ProlepticMonth", MONTHS, FOREVER, ValueRange.of(Year.MIN_VALUE * 12L, Year.MAX_VALUE * 12L + 11)),
     /**
      * The year within the era.
      * <p>
@@ -446,7 +461,7 @@
      * defined with any appropriate value, although defining it to be the same as ISO may be
      * the best option.
      */
-    YEAR("Year", YEARS, FOREVER, ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE)),
+    YEAR("Year", YEARS, FOREVER, ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE), "year"),
     /**
      * The era.
      * <p>
@@ -463,7 +478,7 @@
      * Earlier eras must have sequentially smaller values.
      * Later eras must have sequentially larger values,
      */
-    ERA("Era", ERAS, FOREVER, ValueRange.of(0, 1)),
+    ERA("Era", ERAS, FOREVER, ValueRange.of(0, 1), "era"),
     /**
      * The instant epoch-seconds.
      * <p>
@@ -499,12 +514,23 @@
     private final TemporalUnit baseUnit;
     private final TemporalUnit rangeUnit;
     private final ValueRange range;
+    private final String displayNameKey;
 
     private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) {
         this.name = name;
         this.baseUnit = baseUnit;
         this.rangeUnit = rangeUnit;
         this.range = range;
+        this.displayNameKey = null;
+    }
+
+    private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit,
+            ValueRange range, String displayNameKey) {
+        this.name = name;
+        this.baseUnit = baseUnit;
+        this.rangeUnit = rangeUnit;
+        this.range = range;
+        this.displayNameKey = displayNameKey;
     }
 
     //-----------------------------------------------------------------------
@@ -514,6 +540,20 @@
     }
 
     @Override
+    public String getDisplayName(Locale locale) {
+        Objects.requireNonNull(locale, "locale");
+        if (displayNameKey == null) {
+            return getName();
+        }
+
+        LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
+                                    .getLocaleResources(locale);
+        ResourceBundle rb = lr.getJavaTimeFormatData();
+        String key = "field." + displayNameKey;
+        return rb.containsKey(key) ? rb.getString(key) : getName();
+    }
+
+    @Override
     public TemporalUnit getBaseUnit() {
         return baseUnit;
     }
@@ -548,19 +588,25 @@
     //-----------------------------------------------------------------------
     /**
      * Checks if this field represents a component of a date.
+     * <p>
+     * Fields from day-of-week to era are date-based.
      *
      * @return true if it is a component of a date
      */
-    public boolean isDateField() {
+    @Override
+    public boolean isDateBased() {
         return ordinal() >= DAY_OF_WEEK.ordinal() && ordinal() <= ERA.ordinal();
     }
 
     /**
      * Checks if this field represents a component of a time.
+     * <p>
+     * Fields from nano-of-second to am-pm-of-day are time-based.
      *
      * @return true if it is a component of a time
      */
-    public boolean isTimeField() {
+    @Override
+    public boolean isTimeBased() {
         return ordinal() < DAY_OF_WEEK.ordinal();
     }