提交 30262738 编写于 作者: O okutsu

6902861: (cal) GregorianCalendar roll WEEK_OF_YEAR is broken for January 1 2010

Reviewed-by: peytoia
上级 d8cd5cf6
...@@ -41,11 +41,8 @@ package java.util; ...@@ -41,11 +41,8 @@ package java.util;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.chrono.IsoChronology;
import java.time.temporal.ChronoField; import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQuery;
import sun.util.calendar.BaseCalendar; import sun.util.calendar.BaseCalendar;
import sun.util.calendar.CalendarDate; import sun.util.calendar.CalendarDate;
import sun.util.calendar.CalendarSystem; import sun.util.calendar.CalendarSystem;
...@@ -867,6 +864,7 @@ public class GregorianCalendar extends Calendar { ...@@ -867,6 +864,7 @@ public class GregorianCalendar extends Calendar {
* <code>false</code> otherwise. * <code>false</code> otherwise.
* @see Calendar#compareTo(Calendar) * @see Calendar#compareTo(Calendar)
*/ */
@Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
return obj instanceof GregorianCalendar && return obj instanceof GregorianCalendar &&
super.equals(obj) && super.equals(obj) &&
...@@ -876,6 +874,7 @@ public class GregorianCalendar extends Calendar { ...@@ -876,6 +874,7 @@ public class GregorianCalendar extends Calendar {
/** /**
* Generates the hash code for this <code>GregorianCalendar</code> object. * Generates the hash code for this <code>GregorianCalendar</code> object.
*/ */
@Override
public int hashCode() { public int hashCode() {
return super.hashCode() ^ (int)gregorianCutoverDate; return super.hashCode() ^ (int)gregorianCutoverDate;
} }
...@@ -908,6 +907,7 @@ public class GregorianCalendar extends Calendar { ...@@ -908,6 +907,7 @@ public class GregorianCalendar extends Calendar {
* or if any calendar fields have out-of-range values in * or if any calendar fields have out-of-range values in
* non-lenient mode. * non-lenient mode.
*/ */
@Override
public void add(int field, int amount) { public void add(int field, int amount) {
// If amount == 0, do nothing even the given field is out of // If amount == 0, do nothing even the given field is out of
// range. This is tested by JCK. // range. This is tested by JCK.
...@@ -1106,6 +1106,7 @@ public class GregorianCalendar extends Calendar { ...@@ -1106,6 +1106,7 @@ public class GregorianCalendar extends Calendar {
* @see #add(int,int) * @see #add(int,int)
* @see #set(int,int) * @see #set(int,int)
*/ */
@Override
public void roll(int field, boolean up) { public void roll(int field, boolean up) {
roll(field, up ? +1 : -1); roll(field, up ? +1 : -1);
} }
...@@ -1154,6 +1155,7 @@ public class GregorianCalendar extends Calendar { ...@@ -1154,6 +1155,7 @@ public class GregorianCalendar extends Calendar {
* @see #set(int,int) * @see #set(int,int)
* @since 1.2 * @since 1.2
*/ */
@Override
public void roll(int field, int amount) { public void roll(int field, int amount) {
// If amount == 0, do nothing even the given field is out of // If amount == 0, do nothing even the given field is out of
// range. This is tested by JCK. // range. This is tested by JCK.
...@@ -1272,25 +1274,44 @@ public class GregorianCalendar extends Calendar { ...@@ -1272,25 +1274,44 @@ public class GregorianCalendar extends Calendar {
int woy = internalGet(WEEK_OF_YEAR); int woy = internalGet(WEEK_OF_YEAR);
int value = woy + amount; int value = woy + amount;
if (!isCutoverYear(y)) { if (!isCutoverYear(y)) {
// If the new value is in between min and max int weekYear = getWeekYear();
// (exclusive), then we can use the value. if (weekYear == y) {
if (value > min && value < max) { // If the new value is in between min and max
set(WEEK_OF_YEAR, value); // (exclusive), then we can use the value.
return; if (value > min && value < max) {
} set(WEEK_OF_YEAR, value);
long fd = getCurrentFixedDate(); return;
// Make sure that the min week has the current DAY_OF_WEEK }
long day1 = fd - (7 * (woy - min)); long fd = getCurrentFixedDate();
if (calsys.getYearFromFixedDate(day1) != y) { // Make sure that the min week has the current DAY_OF_WEEK
min++; // 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 // Make sure the same thing for the max week
fd += 7 * (max - internalGet(WEEK_OF_YEAR)); fd += 7 * (max - internalGet(WEEK_OF_YEAR));
if (calsys.getYearFromFixedDate(fd) != y) { if (calsys.getYearFromFixedDate(fd) != y) {
max--; 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;
}
} }
break; set(field, getRolledValue(woy, amount, min, max));
return;
} }
// Handle cutover here. // Handle cutover here.
...@@ -1510,6 +1531,7 @@ public class GregorianCalendar extends Calendar { ...@@ -1510,6 +1531,7 @@ public class GregorianCalendar extends Calendar {
* @see #getActualMinimum(int) * @see #getActualMinimum(int)
* @see #getActualMaximum(int) * @see #getActualMaximum(int)
*/ */
@Override
public int getMinimum(int field) { public int getMinimum(int field) {
return MIN_VALUES[field]; return MIN_VALUES[field];
} }
...@@ -1533,6 +1555,7 @@ public class GregorianCalendar extends Calendar { ...@@ -1533,6 +1555,7 @@ public class GregorianCalendar extends Calendar {
* @see #getActualMinimum(int) * @see #getActualMinimum(int)
* @see #getActualMaximum(int) * @see #getActualMaximum(int)
*/ */
@Override
public int getMaximum(int field) { public int getMaximum(int field) {
switch (field) { switch (field) {
case MONTH: case MONTH:
...@@ -1581,6 +1604,7 @@ public class GregorianCalendar extends Calendar { ...@@ -1581,6 +1604,7 @@ public class GregorianCalendar extends Calendar {
* @see #getActualMinimum(int) * @see #getActualMinimum(int)
* @see #getActualMaximum(int) * @see #getActualMaximum(int)
*/ */
@Override
public int getGreatestMinimum(int field) { public int getGreatestMinimum(int field) {
if (field == DAY_OF_MONTH) { if (field == DAY_OF_MONTH) {
BaseCalendar.Date d = getGregorianCutoverDate(); BaseCalendar.Date d = getGregorianCutoverDate();
...@@ -1610,6 +1634,7 @@ public class GregorianCalendar extends Calendar { ...@@ -1610,6 +1634,7 @@ public class GregorianCalendar extends Calendar {
* @see #getActualMinimum(int) * @see #getActualMinimum(int)
* @see #getActualMaximum(int) * @see #getActualMaximum(int)
*/ */
@Override
public int getLeastMaximum(int field) { public int getLeastMaximum(int field) {
switch (field) { switch (field) {
case MONTH: case MONTH:
...@@ -1659,6 +1684,7 @@ public class GregorianCalendar extends Calendar { ...@@ -1659,6 +1684,7 @@ public class GregorianCalendar extends Calendar {
* @see #getActualMaximum(int) * @see #getActualMaximum(int)
* @since 1.2 * @since 1.2
*/ */
@Override
public int getActualMinimum(int field) { public int getActualMinimum(int field) {
if (field == DAY_OF_MONTH) { if (field == DAY_OF_MONTH) {
GregorianCalendar gc = getNormalizedCalendar(); GregorianCalendar gc = getNormalizedCalendar();
...@@ -1702,6 +1728,7 @@ public class GregorianCalendar extends Calendar { ...@@ -1702,6 +1728,7 @@ public class GregorianCalendar extends Calendar {
* @see #getActualMinimum(int) * @see #getActualMinimum(int)
* @since 1.2 * @since 1.2
*/ */
@Override
public int getActualMaximum(int field) { public int getActualMaximum(int field) {
final int fieldsForFixedMax = ERA_MASK|DAY_OF_WEEK_MASK|HOUR_MASK|AM_PM_MASK| final int fieldsForFixedMax = ERA_MASK|DAY_OF_WEEK_MASK|HOUR_MASK|AM_PM_MASK|
HOUR_OF_DAY_MASK|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK| HOUR_OF_DAY_MASK|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK|
...@@ -1970,6 +1997,7 @@ public class GregorianCalendar extends Calendar { ...@@ -1970,6 +1997,7 @@ public class GregorianCalendar extends Calendar {
(internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET)); (internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET));
} }
@Override
public Object clone() public Object clone()
{ {
GregorianCalendar other = (GregorianCalendar) super.clone(); GregorianCalendar other = (GregorianCalendar) super.clone();
...@@ -1987,6 +2015,7 @@ public class GregorianCalendar extends Calendar { ...@@ -1987,6 +2015,7 @@ public class GregorianCalendar extends Calendar {
return other; return other;
} }
@Override
public TimeZone getTimeZone() { public TimeZone getTimeZone() {
TimeZone zone = super.getTimeZone(); TimeZone zone = super.getTimeZone();
// To share the zone by CalendarDates // To share the zone by CalendarDates
...@@ -1997,6 +2026,7 @@ public class GregorianCalendar extends Calendar { ...@@ -1997,6 +2026,7 @@ public class GregorianCalendar extends Calendar {
return zone; return zone;
} }
@Override
public void setTimeZone(TimeZone zone) { public void setTimeZone(TimeZone zone) {
super.setTimeZone(zone); super.setTimeZone(zone);
// To share the zone by CalendarDates // To share the zone by CalendarDates
...@@ -2227,6 +2257,7 @@ public class GregorianCalendar extends Calendar { ...@@ -2227,6 +2257,7 @@ public class GregorianCalendar extends Calendar {
* @see #getActualMaximum(int) * @see #getActualMaximum(int)
* @since 1.7 * @since 1.7
*/ */
@Override
public int getWeeksInWeekYear() { public int getWeeksInWeekYear() {
GregorianCalendar gc = getNormalizedCalendar(); GregorianCalendar gc = getNormalizedCalendar();
int weekYear = gc.getWeekYear(); int weekYear = gc.getWeekYear();
...@@ -2262,8 +2293,9 @@ public class GregorianCalendar extends Calendar { ...@@ -2262,8 +2293,9 @@ public class GregorianCalendar extends Calendar {
* *
* @see Calendar#complete * @see Calendar#complete
*/ */
@Override
protected void computeFields() { protected void computeFields() {
int mask = 0; int mask;
if (isPartiallyNormalized()) { if (isPartiallyNormalized()) {
// Determine which calendar fields need to be computed. // Determine which calendar fields need to be computed.
mask = getSetStateFields(); mask = getSetStateFields();
...@@ -2598,6 +2630,7 @@ public class GregorianCalendar extends Calendar { ...@@ -2598,6 +2630,7 @@ public class GregorianCalendar extends Calendar {
* *
* @exception IllegalArgumentException if any calendar fields are invalid. * @exception IllegalArgumentException if any calendar fields are invalid.
*/ */
@Override
protected void computeTime() { protected void computeTime() {
// In non-lenient mode, perform brief checking of calendar // In non-lenient mode, perform brief checking of calendar
// fields which have been set externally. Through this // fields which have been set externally. Through this
......
/*
* 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++;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册