src/share/classes/java/util/GregorianCalendar.java

Print this page

        

@@ -39,15 +39,12 @@
 package java.util;
 
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.time.Instant;
-import java.time.ZoneId;
 import java.time.ZonedDateTime;
-import java.time.chrono.IsoChronology;
 import java.time.temporal.ChronoField;
-import java.time.temporal.TemporalQuery;
 import sun.util.calendar.BaseCalendar;
 import sun.util.calendar.CalendarDate;
 import sun.util.calendar.CalendarSystem;
 import sun.util.calendar.CalendarUtils;
 import sun.util.calendar.Era;

@@ -865,19 +862,21 @@
      * @param obj the object to compare with.
      * @return <code>true</code> if this object is equal to <code>obj</code>;
      * <code>false</code> otherwise.
      * @see Calendar#compareTo(Calendar)
      */
+    @Override
     public boolean equals(Object obj) {
         return obj instanceof GregorianCalendar &&
             super.equals(obj) &&
             gregorianCutover == ((GregorianCalendar)obj).gregorianCutover;
     }
 
     /**
      * Generates the hash code for this <code>GregorianCalendar</code> object.
      */
+    @Override
     public int hashCode() {
         return super.hashCode() ^ (int)gregorianCutoverDate;
     }
 
     /**

@@ -906,10 +905,11 @@
      * @exception IllegalArgumentException if <code>field</code> is
      * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,
      * or if any calendar fields have out-of-range values in
      * non-lenient mode.
      */
+    @Override
     public void add(int field, int amount) {
         // If amount == 0, do nothing even the given field is out of
         // range. This is tested by JCK.
         if (amount == 0) {
             return;   // Do nothing!

@@ -1104,10 +1104,11 @@
      * or if any calendar fields have out-of-range values in
      * non-lenient mode.
      * @see #add(int,int)
      * @see #set(int,int)
      */
+    @Override
     public void roll(int field, boolean up) {
         roll(field, up ? +1 : -1);
     }
 
     /**

@@ -1152,10 +1153,11 @@
      * @see #roll(int,boolean)
      * @see #add(int,int)
      * @see #set(int,int)
      * @since 1.2
      */
+    @Override
     public void roll(int field, int amount) {
         // If amount == 0, do nothing even the given field is out of
         // range. This is tested by JCK.
         if (amount == 0) {
             return;

@@ -1270,29 +1272,48 @@
                 max = getActualMaximum(WEEK_OF_YEAR);
                 set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));
                 int woy = internalGet(WEEK_OF_YEAR);
                 int value = woy + amount;
                 if (!isCutoverYear(y)) {
+                    int weekYear = getWeekYear();
+                    if (weekYear == y) {
                     // If the new value is in between min and max
                     // (exclusive), then we can use the value.
                     if (value > min && value < max) {
                         set(WEEK_OF_YEAR, value);
                         return;
                     }
                     long fd = getCurrentFixedDate();
                     // Make sure that the min week has the current DAY_OF_WEEK
+                        // in the calendar year
                     long day1 = fd - (7 * (woy - min));
                     if (calsys.getYearFromFixedDate(day1) != y) {
                         min++;
                     }
 
                     // Make sure the same thing for the max week
                     fd += 7 * (max - internalGet(WEEK_OF_YEAR));
                     if (calsys.getYearFromFixedDate(fd) != y) {
                         max--;
                     }
-                    break;
+                    } else {
+                        // When WEEK_OF_YEAR and YEAR are out of sync,
+                        // adjust woy and amount to stay in the calendar year.
+                        if (weekYear > y) {
+                            if (amount < 0) {
+                                amount++;
+                            }
+                            woy = max;
+                        } else {
+                            if (amount > 0) {
+                                amount -= woy - max;
+                            }
+                            woy = min;
+                        }
+                    }
+                    set(field, getRolledValue(woy, amount, min, max));
+                    return;
                 }
 
                 // Handle cutover here.
                 long fd = getCurrentFixedDate();
                 BaseCalendar cal;

@@ -1508,10 +1529,11 @@
      * @see #getGreatestMinimum(int)
      * @see #getLeastMaximum(int)
      * @see #getActualMinimum(int)
      * @see #getActualMaximum(int)
      */
+    @Override
     public int getMinimum(int field) {
         return MIN_VALUES[field];
     }
 
     /**

@@ -1531,10 +1553,11 @@
      * @see #getGreatestMinimum(int)
      * @see #getLeastMaximum(int)
      * @see #getActualMinimum(int)
      * @see #getActualMaximum(int)
      */
+    @Override
     public int getMaximum(int field) {
         switch (field) {
         case MONTH:
         case DAY_OF_MONTH:
         case DAY_OF_YEAR:

@@ -1579,10 +1602,11 @@
      * @see #getMaximum(int)
      * @see #getLeastMaximum(int)
      * @see #getActualMinimum(int)
      * @see #getActualMaximum(int)
      */
+    @Override
     public int getGreatestMinimum(int field) {
         if (field == DAY_OF_MONTH) {
             BaseCalendar.Date d = getGregorianCutoverDate();
             long mon1 = getFixedDateMonth1(d, gregorianCutoverDate);
             d = getCalendarDate(mon1);

@@ -1608,10 +1632,11 @@
      * @see #getMaximum(int)
      * @see #getGreatestMinimum(int)
      * @see #getActualMinimum(int)
      * @see #getActualMaximum(int)
      */
+    @Override
     public int getLeastMaximum(int field) {
         switch (field) {
         case MONTH:
         case DAY_OF_MONTH:
         case DAY_OF_YEAR:

@@ -1657,10 +1682,11 @@
      * @see #getGreatestMinimum(int)
      * @see #getLeastMaximum(int)
      * @see #getActualMaximum(int)
      * @since 1.2
      */
+    @Override
     public int getActualMinimum(int field) {
         if (field == DAY_OF_MONTH) {
             GregorianCalendar gc = getNormalizedCalendar();
             int year = gc.cdate.getNormalizedYear();
             if (year == gregorianCutoverYear || year == gregorianCutoverYearJulian) {

@@ -1700,10 +1726,11 @@
      * @see #getGreatestMinimum(int)
      * @see #getLeastMaximum(int)
      * @see #getActualMinimum(int)
      * @since 1.2
      */
+    @Override
     public int getActualMaximum(int field) {
         final int fieldsForFixedMax = ERA_MASK|DAY_OF_WEEK_MASK|HOUR_MASK|AM_PM_MASK|
             HOUR_OF_DAY_MASK|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK|
             ZONE_OFFSET_MASK|DST_OFFSET_MASK;
         if ((fieldsForFixedMax & (1<<field)) != 0) {

@@ -1968,10 +1995,11 @@
         t *= 1000;
         return t + internalGet(MILLISECOND) -
             (internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET));
     }
 
+    @Override
     public Object clone()
     {
         GregorianCalendar other = (GregorianCalendar) super.clone();
 
         other.gdate = (BaseCalendar.Date) gdate.clone();

@@ -1985,20 +2013,22 @@
         other.originalFields = null;
         other.zoneOffsets = null;
         return other;
     }
 
+    @Override
     public TimeZone getTimeZone() {
         TimeZone zone = super.getTimeZone();
         // To share the zone by CalendarDates
         gdate.setZone(zone);
         if (cdate != null && cdate != gdate) {
             cdate.setZone(zone);
         }
         return zone;
     }
 
+    @Override
     public void setTimeZone(TimeZone zone) {
         super.setTimeZone(zone);
         // To share the zone by CalendarDates
         gdate.setZone(zone);
         if (cdate != null && cdate != gdate) {

@@ -2225,10 +2255,11 @@
      * @see Calendar#WEEK_OF_YEAR
      * @see #getWeekYear()
      * @see #getActualMaximum(int)
      * @since 1.7
      */
+    @Override
     public int getWeeksInWeekYear() {
         GregorianCalendar gc = getNormalizedCalendar();
         int weekYear = gc.getWeekYear();
         if (weekYear == gc.internalGet(YEAR)) {
             return gc.getActualMaximum(WEEK_OF_YEAR);

@@ -2260,12 +2291,13 @@
      * recomputed first; to recompute the time, then the fields, call the
      * <code>complete</code> method.
      *
      * @see Calendar#complete
      */
+    @Override
     protected void computeFields() {
-        int mask = 0;
+        int mask;
         if (isPartiallyNormalized()) {
             // Determine which calendar fields need to be computed.
             mask = getSetStateFields();
             int fieldMask = ~mask & ALL_FIELDS;
             // We have to call computTime in case calsys == null in

@@ -2596,10 +2628,11 @@
      * Converts calendar field values to the time value (millisecond
      * offset from the <a href="Calendar.html#Epoch">Epoch</a>).
      *
      * @exception IllegalArgumentException if any calendar fields are invalid.
      */
+    @Override
     protected void computeTime() {
         // In non-lenient mode, perform brief checking of calendar
         // fields which have been set externally. Through this
         // checking, the field values are stored in originalFields[]
         // to see if any of them are normalized later.