Path: blob/master/test/jdk/java/util/Calendar/Bug4851640.java
41149 views
/*1* Copyright (c) 2003, 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 485164026* @summary Make sure not to set UNSET fields to COMPUTED after time calculation.27*/2829import java.util.GregorianCalendar;30import static java.util.Calendar.*;3132public class Bug4851640 {3334public static void main(String args[]) {35GregorianCalendar cal = new GregorianCalendar();36cal.clear();37cal.set(YEAR, 2003);38long t = cal.getTime().getTime();3940// For the time calculation, the MONTH and DAY_OF_MONTH fields41// (with the default values) have been used for determining42// the date. However, both the MONTH and DAY_OF_MONTH fields43// should be kept UNSET after the time calculation.44if (cal.isSet(MONTH) || cal.isSet(DAY_OF_MONTH)) {45throw new RuntimeException("After getTime(): MONTH field=" + cal.isSet(MONTH)46+ ", DAY_OF_MONTH field=" + cal.isSet(DAY_OF_MONTH));47}4849// After calling get() for any field, all field values are50// recalculated and their field states are set to51// COMPUTED. isSet() must return true.52int y = cal.get(YEAR);53if (!(cal.isSet(MONTH) && cal.isSet(DAY_OF_MONTH))) {54throw new RuntimeException("After get(): MONTH field=" + cal.isSet(MONTH)55+ ", DAY_OF_MONTH field=" + cal.isSet(DAY_OF_MONTH));56}57}58}596061