1/* 2 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 6234795 27 * @summary Rolling of HOUR or HOUR_OF_SET must set the other hour field. 28 */ 29 30import java.util.GregorianCalendar; 31import static java.util.Calendar.AM; 32import static java.util.Calendar.AM_PM; 33import static java.util.Calendar.HOUR; 34import static java.util.Calendar.HOUR_OF_DAY; 35import static java.util.Calendar.SEPTEMBER; 36import java.util.Locale; 37import java.util.TimeZone; 38 39public class Bug6234795 { 40 public static void main(String[] args) { 41 testRoll(HOUR); 42 testRoll(HOUR_OF_DAY); 43 } 44 45 static void testRoll(int field) { 46 GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.US); 47 cal.clear(); 48 cal.set(2005, SEPTEMBER, 12); 49 50 int otherField = (field == HOUR) ? HOUR_OF_DAY : HOUR; 51 int unit = (field == HOUR) ? 12 : 24; 52 int h; 53 for (h = 0; h <= 72; h++) { 54 int hour = cal.get(otherField); 55 int expected = h % 12; 56 if (hour != expected) { 57 throw new RuntimeException((field == HOUR ? "HOUR" : "HOUR_OF_DAY") 58 + "+: h=" + h + ", got " + hour 59 + ", expected " + expected); 60 } 61 if (field == HOUR_OF_DAY) { 62 int ampm = cal.get(AM_PM); 63 expected = (h % unit) / 12; 64 if (ampm != expected) { 65 throw new RuntimeException((field == HOUR ? "HOUR" : "HOUR_OF_DAY") 66 + "+: h=" + h + ", got " 67 + toString(ampm) 68 + ", expected " + toString(expected)); 69 } 70 } 71 cal.roll(field, +1); 72 } 73 for (; h >= 0; h--) { 74 int hour = cal.get(otherField); 75 int expected = h % 12; 76 if (hour != expected) { 77 throw new RuntimeException((field == HOUR ? "HOUR" : "HOUR_OF_DAY") 78 + "-: h=" + h + ", got " + hour 79 + ", expected " + expected); 80 } 81 if (field == HOUR_OF_DAY) { 82 int ampm = cal.get(AM_PM); 83 expected = (h % unit) / 12; 84 if (ampm != expected) { 85 throw new RuntimeException((field == HOUR ? "HOUR" : "HOUR_OF_DAY") 86 + "-: h=" + h + ", got " + toString(ampm) 87 + ", expected " + toString(expected)); 88 } 89 } 90 cal.roll(field, -1); 91 } 92 } 93 94 static String toString(int ampm) { 95 return ampm == AM ? "AM" : "PM"; 96 } 97} 98