Path: blob/master/test/jdk/java/util/Calendar/Bug8152077.java
41149 views
/*1* Copyright (c) 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 815207726* @summary Make sure that roll() with HOUR/HOUR_OF_DAY works around standard/daylight27* time transitions28*/2930import java.util.Calendar;31import java.util.GregorianCalendar;32import java.util.TimeZone;33import static java.util.Calendar.*;3435public class Bug8152077 {36private static final TimeZone LA = TimeZone.getTimeZone("America/Los_Angeles");37private static final TimeZone BR = TimeZone.getTimeZone("America/Sao_Paulo");3839private static final int[] ALLDAY_HOURS = {400, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,4112, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 2342};43private static final int[] AM_HOURS = {440, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1145};46private static final int[] PM_HOURS = { // in 24-hour clock4712, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 2348};4950private static int errors;5152public static void main(String[] args) {53TimeZone initialTz = TimeZone.getDefault();54try {55testRoll(LA, new int[] { 2016, MARCH, 13 },56new int[] { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11,5712, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 });58testRoll(LA, new int[] { 2016, MARCH, 13 },59new int[] { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11 });60testRoll(LA, new int[] { 2016, MARCH, 13 }, PM_HOURS);6162testRoll(LA, new int[] { 2016, NOVEMBER, 6 }, ALLDAY_HOURS);63testRoll(LA, new int[] { 2016, NOVEMBER, 6 }, AM_HOURS);64testRoll(LA, new int[] { 2016, NOVEMBER, 6 }, PM_HOURS);6566testRoll(BR, new int[] { 2016, FEBRUARY, 21 }, ALLDAY_HOURS);67testRoll(BR, new int[] { 2016, FEBRUARY, 21 }, AM_HOURS);68testRoll(BR, new int[] { 2016, FEBRUARY, 21 }, PM_HOURS);6970testRoll(BR, new int[] { 2016, OCTOBER, 15 }, ALLDAY_HOURS);71testRoll(BR, new int[] { 2016, OCTOBER, 15 }, PM_HOURS);72testRoll(BR, new int[] { 2016, OCTOBER, 16 },73new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,7412, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 });75testRoll(BR, new int[] { 2016, OCTOBER, 16 },76new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });77testRoll(BR, new int[] { 2016, OCTOBER, 16 }, PM_HOURS);78} finally {79TimeZone.setDefault(initialTz);80}81if (errors > 0) {82throw new RuntimeException("Test failed");83}84}8586private static void testRoll(TimeZone tz, int[] params, int[] sequence) {87TimeZone.setDefault(tz);88for (int i = 0; i < sequence.length; i++) {89testRoll(+1, params, sequence, i);90testRoll(-1, params, sequence, i);91}92}9394// amount must be 1 or -1.95private static void testRoll(int amount, int[] params, int[] sequence, int startIndex) {96int year = params[0];97int month = params[1];98int dayOfMonth = params[2];99int hourOfDay = sequence[startIndex];100Calendar cal = new GregorianCalendar(year, month, dayOfMonth,101hourOfDay, 0, 0);102int ampm = cal.get(AM_PM);103104int length = sequence.length;105int count = length * 2;106int field = (length > 12) ? HOUR_OF_DAY : HOUR;107108System.out.printf("roll(%s, %2d) starting from %s%n",109(field == HOUR) ? "HOUR" : "HOUR_OF_DAY", amount, cal.getTime());110for (int i = 0; i < count; i++) {111int index = (amount > 0) ? (startIndex + i + 1) % length112: Math.floorMod(startIndex - i - 1, length);113int expected = sequence[index];114if (field == HOUR) {115expected %= 12;116}117cal.roll(field, amount);118int value = cal.get(field);119if (value != expected) {120System.out.println("Unexpected field value: got=" + value121+ ", expected=" + expected);122errors++;123}124if (cal.get(DAY_OF_MONTH) != dayOfMonth) {125System.out.println("DAY_OF_MONTH changed: " + dayOfMonth126+ " to " + cal.get(DAY_OF_MONTH));127}128if (field == HOUR && cal.get(AM_PM) != ampm) {129System.out.println("AM_PM changed: " + ampm + " to " + cal.get(AM_PM));130errors++;131}132}133}134}135136137