Path: blob/master/test/jdk/java/util/Calendar/Bug6234795.java
41149 views
/*1* Copyright (c) 2005, 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 623479526* @summary Rolling of HOUR or HOUR_OF_SET must set the other hour field.27*/2829import java.util.GregorianCalendar;30import static java.util.Calendar.AM;31import static java.util.Calendar.AM_PM;32import static java.util.Calendar.HOUR;33import static java.util.Calendar.HOUR_OF_DAY;34import static java.util.Calendar.SEPTEMBER;35import java.util.Locale;36import java.util.TimeZone;3738public class Bug6234795 {39public static void main(String[] args) {40testRoll(HOUR);41testRoll(HOUR_OF_DAY);42}4344static void testRoll(int field) {45GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.US);46cal.clear();47cal.set(2005, SEPTEMBER, 12);4849int otherField = (field == HOUR) ? HOUR_OF_DAY : HOUR;50int unit = (field == HOUR) ? 12 : 24;51int h;52for (h = 0; h <= 72; h++) {53int hour = cal.get(otherField);54int expected = h % 12;55if (hour != expected) {56throw new RuntimeException((field == HOUR ? "HOUR" : "HOUR_OF_DAY")57+ "+: h=" + h + ", got " + hour58+ ", expected " + expected);59}60if (field == HOUR_OF_DAY) {61int ampm = cal.get(AM_PM);62expected = (h % unit) / 12;63if (ampm != expected) {64throw new RuntimeException((field == HOUR ? "HOUR" : "HOUR_OF_DAY")65+ "+: h=" + h + ", got "66+ toString(ampm)67+ ", expected " + toString(expected));68}69}70cal.roll(field, +1);71}72for (; h >= 0; h--) {73int hour = cal.get(otherField);74int expected = h % 12;75if (hour != expected) {76throw new RuntimeException((field == HOUR ? "HOUR" : "HOUR_OF_DAY")77+ "-: h=" + h + ", got " + hour78+ ", expected " + expected);79}80if (field == HOUR_OF_DAY) {81int ampm = cal.get(AM_PM);82expected = (h % unit) / 12;83if (ampm != expected) {84throw new RuntimeException((field == HOUR ? "HOUR" : "HOUR_OF_DAY")85+ "-: h=" + h + ", got " + toString(ampm)86+ ", expected " + toString(expected));87}88}89cal.roll(field, -1);90}91}9293static String toString(int ampm) {94return ampm == AM ? "AM" : "PM";95}96}979899