Path: blob/master/test/jdk/java/util/Calendar/RollDayOfWeekTest.java
41149 views
/*1* Copyright (c) 2004, 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 5090555 509180526* @summary Make sure that rolling DAY_OF_WEEK stays in the same week27* around year boundaries.28* @run main/othervm RollDayOfWeekTest 5 529*/3031import java.util.*;3233import static java.util.Calendar.*;3435// Usage: java RollDayOfWeekTest [pastYears futureYears]36public class RollDayOfWeekTest {37public static void main(String[] args) {38int pastYears = 5, futureYears = 23;39if (args.length == 2) {40pastYears = Integer.parseInt(args[0]);41pastYears = Math.max(1, Math.min(pastYears, 5));42futureYears = Integer.parseInt(args[1]);43futureYears = Math.max(1, Math.min(futureYears, 28));44}4546System.out.printf("Test [%d .. %+d] year range.%n", -pastYears, futureYears);47Calendar cal = new GregorianCalendar();48int year = cal.get(YEAR) - pastYears;4950// Use the all combinations of firstDayOfWeek and51// minimalDaysInFirstWeek values in the year range current52// year - pastYears to current year + futureYears.53for (int fdw = SUNDAY; fdw <= SATURDAY; fdw++) {54for (int mdifw = 1; mdifw <= 7; mdifw++) {55cal.clear();56cal.setFirstDayOfWeek(fdw);57cal.setMinimalDaysInFirstWeek(mdifw);58cal.set(year, JANUARY, 1);59checkRoll(cal, futureYears);60}61}6263// testing roll from BCE to CE64year = -1;65for (int fdw = SUNDAY; fdw <= SATURDAY; fdw++) {66for (int mdifw = 1; mdifw <= 7; mdifw++) {67cal.clear();68cal.setFirstDayOfWeek(fdw);69cal.setMinimalDaysInFirstWeek(mdifw);70cal.set(year, JANUARY, 1);71checkRoll(cal, 4);72}73}74}757677static void checkRoll(Calendar cal, int years) {78Calendar cal2 = null, cal3 = null, prev = null;79// Check 28 years80for (int x = 0; x < (int)(365.2425*years); x++) {81cal2 = (Calendar) cal.clone();82cal3 = (Calendar) cal.clone();8384// roll foreword85for (int i = 0; i < 10; i++) {86prev = (Calendar) cal2.clone();87cal2.roll(Calendar.DAY_OF_WEEK, +1);88roll(cal3, +1);89long t2 = cal2.getTimeInMillis();90long t3 = cal3.getTimeInMillis();91if (t2 != t3) {92System.err.println("prev: " + prev.getTime() + "\n" + prev);93System.err.println("cal2: " + cal2.getTime() + "\n" + cal2);94System.err.println("cal3: " + cal3.getTime() + "\n" + cal3);95throw new RuntimeException("+1: t2=" + t2 + ", t3=" + t3);96}97}9899// roll backward100for (int i = 0; i < 10; i++) {101prev = (Calendar) cal2.clone();102cal2.roll(Calendar.DAY_OF_WEEK, -1);103roll(cal3, -1);104long t2 = cal2.getTimeInMillis();105long t3 = cal3.getTimeInMillis();106if (t2 != t3) {107System.err.println("prev: " + prev.getTime() + "\n" + prev);108System.err.println("cal2: " + cal2.getTime() + "\n" + cal2);109System.err.println("cal3: " + cal3.getTime() + "\n" + cal3);110throw new RuntimeException("-1: t2=" + t2 + ", t3=" + t3);111}112}113cal.add(DAY_OF_YEAR, +1);114}115}116117// Another way to roll within the same week.118static void roll(Calendar cal, int n) {119int doy = cal.get(DAY_OF_YEAR);120int diff = cal.get(DAY_OF_WEEK) - cal.getFirstDayOfWeek();121if (diff < 0) {122diff += 7;123}124125// dow1: first day of the week126int dow1 = doy - diff;127n %= 7;128doy += n;129if (doy < dow1) {130doy += 7;131} else if (doy >= dow1 + 7) {132doy -= 7;133}134cal.set(DAY_OF_YEAR, doy);135}136}137138139