# HG changeset patch # User okutsu # Date 1380695495 -32400 # Node ID 44c2cb8a99bb5fde7fd756c6e853a5559ac0c532 # Parent 4aa3d51ec41b728b1f7faffb610af874bac1dd5b 6902861: (cal) GregorianCalendar roll WEEK_OF_YEAR is broken for January 1 2010 Reviewed-by: peytoia diff -r 4aa3d51ec41b -r 44c2cb8a99bb jdk/src/share/classes/java/util/GregorianCalendar.java --- a/jdk/src/share/classes/java/util/GregorianCalendar.java Tue Oct 01 20:25:44 2013 -0700 +++ b/jdk/src/share/classes/java/util/GregorianCalendar.java Wed Oct 02 15:31:35 2013 +0900 @@ -41,11 +41,8 @@ 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; @@ -867,6 +864,7 @@ * false otherwise. * @see Calendar#compareTo(Calendar) */ + @Override public boolean equals(Object obj) { return obj instanceof GregorianCalendar && super.equals(obj) && @@ -876,6 +874,7 @@ /** * Generates the hash code for this GregorianCalendar object. */ + @Override public int hashCode() { return super.hashCode() ^ (int)gregorianCutoverDate; } @@ -908,6 +907,7 @@ * 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. @@ -1106,6 +1106,7 @@ * @see #add(int,int) * @see #set(int,int) */ + @Override public void roll(int field, boolean up) { roll(field, up ? +1 : -1); } @@ -1154,6 +1155,7 @@ * @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. @@ -1272,25 +1274,44 @@ int woy = internalGet(WEEK_OF_YEAR); int value = woy + amount; if (!isCutoverYear(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; + 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--; + } + } 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; + } } - long fd = getCurrentFixedDate(); - // Make sure that the min week has the current DAY_OF_WEEK - 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; + set(field, getRolledValue(woy, amount, min, max)); + return; } // Handle cutover here. @@ -1510,6 +1531,7 @@ * @see #getActualMinimum(int) * @see #getActualMaximum(int) */ + @Override public int getMinimum(int field) { return MIN_VALUES[field]; } @@ -1533,6 +1555,7 @@ * @see #getActualMinimum(int) * @see #getActualMaximum(int) */ + @Override public int getMaximum(int field) { switch (field) { case MONTH: @@ -1581,6 +1604,7 @@ * @see #getActualMinimum(int) * @see #getActualMaximum(int) */ + @Override public int getGreatestMinimum(int field) { if (field == DAY_OF_MONTH) { BaseCalendar.Date d = getGregorianCutoverDate(); @@ -1610,6 +1634,7 @@ * @see #getActualMinimum(int) * @see #getActualMaximum(int) */ + @Override public int getLeastMaximum(int field) { switch (field) { case MONTH: @@ -1659,6 +1684,7 @@ * @see #getActualMaximum(int) * @since 1.2 */ + @Override public int getActualMinimum(int field) { if (field == DAY_OF_MONTH) { GregorianCalendar gc = getNormalizedCalendar(); @@ -1702,6 +1728,7 @@ * @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| @@ -1970,6 +1997,7 @@ (internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET)); } + @Override public Object clone() { GregorianCalendar other = (GregorianCalendar) super.clone(); @@ -1987,6 +2015,7 @@ return other; } + @Override public TimeZone getTimeZone() { TimeZone zone = super.getTimeZone(); // To share the zone by CalendarDates @@ -1997,6 +2026,7 @@ return zone; } + @Override public void setTimeZone(TimeZone zone) { super.setTimeZone(zone); // To share the zone by CalendarDates @@ -2227,6 +2257,7 @@ * @see #getActualMaximum(int) * @since 1.7 */ + @Override public int getWeeksInWeekYear() { GregorianCalendar gc = getNormalizedCalendar(); int weekYear = gc.getWeekYear(); @@ -2262,8 +2293,9 @@ * * @see Calendar#complete */ + @Override protected void computeFields() { - int mask = 0; + int mask; if (isPartiallyNormalized()) { // Determine which calendar fields need to be computed. mask = getSetStateFields(); @@ -2598,6 +2630,7 @@ * * @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 diff -r 4aa3d51ec41b -r 44c2cb8a99bb jdk/test/java/util/Calendar/Bug6902861.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/util/Calendar/Bug6902861.java Wed Oct 02 15:31:35 2013 +0900 @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 6902861 + * @summary Test for a workaround when WEEK_OF_YEAR and YEAR are out of sync. + */ + +import java.util.*; +import static java.util.GregorianCalendar.*; + +public class Bug6902861 { + static int errors = 0; + + public static void main(String [] args) { + Locale loc = Locale.getDefault(); + try { + Locale.setDefault(Locale.GERMANY); + test(2010, JANUARY, 1, +1, 1); + test(2010, JANUARY, 1, +2, 2); + test(2010, JANUARY, 1, -1, 52); + test(2010, JANUARY, 1, -2, 51); + test(2008, DECEMBER, 31, +1, 1); + test(2008, DECEMBER, 31, +2, 2); + test(2008, DECEMBER, 31, -1, 52); + test(2008, DECEMBER, 31, -2, 51); + if (errors > 0) { + throw new RuntimeException("Failed"); + } + } finally { + Locale.setDefault(loc); + } + } + + static void test(int year, int month, int dayOfMonth, int amount, int expected) { + Calendar calendar = new GregorianCalendar(year, month, dayOfMonth); + int week = calendar.get(WEEK_OF_YEAR); // fix the date + calendar.roll(WEEK_OF_YEAR, amount); + int got = calendar.get(WEEK_OF_YEAR); + int y = calendar.get(YEAR); + if (got != expected || y != year) { + String date = String.format("%04d-%02d-%02d", year, month+1, dayOfMonth); + System.err.printf("%s: roll %+d: got: %d,%2d; expected: %d,%2d%n", + date, amount, y, got, year, expected); + errors++; + } + } +}