Path: blob/master/test/jdk/java/util/Calendar/Bug5078053.java
41149 views
/*1* Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 507805326* @summary Make sure that Calendar.complete() normalizes stamp[] to27* COMPUTED. This can be observed through add() and roll().28*/2930import java.util.TimeZone;31import static java.util.Calendar.*;3233public class Bug5078053 {34static int errorCount = 0;3536public static void main(String[] args) {37TimeZone defaultTz = TimeZone.getDefault();3839try {40TimeZone tz = TimeZone.getTimeZone("Australia/Adelaide");41TimeZone.setDefault(tz);42Koyomi cal = new Koyomi();43cal.setFirstDayOfWeek(2);44cal.setMinimalDaysInFirstWeek(4);4546// test roll()47cal.clear();48// 2002-01-01T00:00:00 in Australia/Adelaide49cal.setTimeInMillis(1009805400000L);50System.out.println(cal.getTime());51// The following set calls shouldn't affect roll() and add()52cal.set(DAY_OF_WEEK, cal.get(DAY_OF_WEEK));53cal.set(WEEK_OF_YEAR, cal.get(WEEK_OF_YEAR));54cal.getTime();55cal.roll(MONTH, +1);56System.out.println("roll: " + cal.getTime());57if (!cal.checkDate(2002, FEBRUARY, 1)) {58error("roll(MONTH, +1): " + cal.getMessage());59}60cal.roll(MONTH, -1);61if (!cal.checkDate(2002, JANUARY, 1)) {62error("roll(MONTH, -1): " + cal.getMessage());63}6465// test add()66cal.clear();67// 2002-01-01T00:00:00+0930 in Australia/Adelaide68cal.setTimeInMillis(1009805400000L);69cal.set(DAY_OF_WEEK, cal.get(DAY_OF_WEEK));70cal.set(WEEK_OF_YEAR, cal.get(WEEK_OF_YEAR));71cal.getTime();72cal.add(MONTH, +1);73System.out.println(" add: " + cal.getTime());74if (!cal.checkDate(2002, FEBRUARY, 1)) {75error("add(MONTH, +1): " + cal.getMessage());76}77cal.add(MONTH, -1);78if (!cal.checkDate(2002, JANUARY, 1)) {79error("add(MONTH, -1): " + cal.getMessage());80}81}82finally {83TimeZone.setDefault(defaultTz);84}8586checkErrors();87}8889static void error(String s) {90System.out.println(s);91errorCount++;92}9394static void checkErrors() {95if (errorCount > 0) {96throw new RuntimeException("Failed: " + errorCount + " error(s)");97}98}99}100101102