1 /*
  2  * Copyright (c) 2002, 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 4514831
 27  * @summary Confirm that GregorianCalendar.roll() works properly during transition from Daylight Saving Time to Standard Time.
 28  */
 29 
 30 import java.util.GregorianCalendar;
 31 import java.util.Locale;
 32 import java.util.TimeZone;
 33 
 34 import static java.util.GregorianCalendar.*;
 35 
 36 
 37 public class bug4514831 {
 38 
 39     public static void main(String[] args) {
 40         Locale savedLocale = Locale.getDefault();
 41         TimeZone savedTimeZone = TimeZone.getDefault();
 42         boolean err = false;
 43 
 44         String golden_data1 = "27-28 28-29 29-30 30-31 31-1 1-2 2-3 ";
 45         String golden_data2 = "27-28 28-29 29-30 30-31 31-25 25-26 26-27 ";
 46         String golden_data3 = "1-8 8-15 15-22 22-29 29-1 1-8 8-15 ";
 47 
 48         try {
 49             Locale.setDefault(Locale.US);
 50             TimeZone.setDefault(TimeZone.getTimeZone("US/Pacific"));
 51 
 52             String test_roll = "";
 53             GregorianCalendar c_roll = new GregorianCalendar(2001, OCTOBER, 27);
 54             for (int i = 0; i < 7; i++) {
 55                 test_roll += c_roll.get(DAY_OF_MONTH) + "-";
 56                 c_roll.roll(DAY_OF_YEAR, true);
 57                 test_roll += c_roll.get(DAY_OF_MONTH) + " ";
 58             }
 59             if (!test_roll.equals(golden_data1)) {
 60                 err = true;
 61                 System.err.println("Wrong roll(DAY_OF_YEAR) transition: got "
 62                         + test_roll + "expected " + golden_data1);
 63             }
 64 
 65             test_roll = "";
 66             c_roll = new GregorianCalendar(2001, OCTOBER, 27);
 67             c_roll.setFirstDayOfWeek(THURSDAY);
 68             for (int i = 0; i < 7; i++) {
 69                 test_roll += c_roll.get(DAY_OF_MONTH) + "-";
 70                 c_roll.roll(DAY_OF_WEEK, true);
 71                 test_roll += c_roll.get(DAY_OF_MONTH) + " ";
 72             }
 73             if (!test_roll.equals(golden_data2)) {
 74                 err = true;
 75                 System.err.println("Wrong roll(DAY_OF_WEEK) transition: got "
 76                         + test_roll + "expected " + golden_data2);
 77             }
 78 
 79             test_roll = "";
 80             c_roll = new GregorianCalendar(2001, OCTOBER, 1);
 81             for (int i = 0; i < 7; i++) {
 82                 test_roll += c_roll.get(DAY_OF_MONTH) + "-";
 83                 c_roll.roll(DAY_OF_WEEK_IN_MONTH, true);
 84                 test_roll += c_roll.get(DAY_OF_MONTH) + " ";
 85             }
 86             if (!test_roll.equals(golden_data3)) {
 87                 err = true;
 88                 System.err.println("Wrong roll(DAY_OF_WEEK_IN_MONTH) transition: got "
 89                         + test_roll + "expected " + golden_data3);
 90             }
 91         } finally {
 92             Locale.setDefault(savedLocale);
 93             TimeZone.setDefault(savedTimeZone);
 94         }
 95 
 96         if (err) {
 97             throw new RuntimeException("Wrong roll() transition");
 98         }
 99     }
100 }