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