1 /* 2 * Copyright (c) 2011, 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 6772689 27 * @summary Test for standard-to-daylight transitions at midnight: 28 * date stays on the given day. 29 */ 30 31 import java.util.Calendar; 32 import java.util.Date; 33 import java.util.GregorianCalendar; 34 import java.util.TimeZone; 35 import static java.util.GregorianCalendar.*; 36 37 public class Bug6772689 { 38 private static final int BEGIN_YEAR = 2035; 39 private static final int END_YEAR = BEGIN_YEAR + 28; 40 41 public static void main(String[] args) { 42 TimeZone defaultTimeZone = TimeZone.getDefault(); 43 int errors = 0; 44 45 Calendar cal = new GregorianCalendar(BEGIN_YEAR, MARCH, 1); 46 String[] tzids = TimeZone.getAvailableIDs(); 47 try { 48 for (String id : tzids) { 49 TimeZone tz = TimeZone.getTimeZone(id); 50 if (!tz.useDaylightTime()) { 51 continue; 52 } 53 TimeZone.setDefault(tz); 54 55 dateloop: 56 // Use future dates because sun.util.calendar.ZoneInfo 57 // delegates offset transition calculations to a SimpleTimeZone 58 // (after 2038 as of JDK7). 59 for (int year = BEGIN_YEAR; year < END_YEAR; year++) { 60 for (int month = MARCH; month <= NOVEMBER; month++) { 61 cal.set(year, month, 1, 15, 0, 0); 62 int maxDom = cal.getActualMaximum(DAY_OF_MONTH); 63 for (int dom = 1; dom <= maxDom; dom++) { 64 Date date = new Date(year - 1900, month, dom); 65 if (date.getYear()+1900 != year 66 || date.getMonth() != month 67 || date.getDate() != dom) { 68 System.err.printf("%s: got %04d-%02d-%02d, expected %04d-%02d-%02d%n", 69 id, 70 date.getYear() + 1900, 71 date.getMonth() + 1, 72 date.getDate(), 73 year, 74 month + 1, 75 dom); 76 errors++; 77 break dateloop; 78 } 79 } 80 } 81 } 82 } 83 } finally { 84 // Restore the default TimeZone. 85 TimeZone.setDefault(defaultTimeZone); 86 } 87 if (errors > 0) { 88 throw new RuntimeException("Transition test failed"); 89 } 90 } 91 }