1 /* 2 * Copyright (c) 2003, 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 4851640 27 * @summary Make sure not to set UNSET fields to COMPUTED after time calculation. 28 */ 29 30 import java.util.GregorianCalendar; 31 import static java.util.Calendar.*; 32 33 public class Bug4851640 { 34 35 public static void main(String args[]) { 36 GregorianCalendar cal = new GregorianCalendar(); 37 cal.clear(); 38 cal.set(YEAR, 2003); 39 long t = cal.getTime().getTime(); 40 41 // For the time calculation, the MONTH and DAY_OF_MONTH fields 42 // (with the default values) have been used for determining 43 // the date. However, both the MONTH and DAY_OF_MONTH fields 44 // should be kept UNSET after the time calculation. 45 if (cal.isSet(MONTH) || cal.isSet(DAY_OF_MONTH)) { 46 throw new RuntimeException("After getTime(): MONTH field=" + cal.isSet(MONTH) 47 + ", DAY_OF_MONTH field=" + cal.isSet(DAY_OF_MONTH)); 48 } 49 50 // After calling get() for any field, all field values are 51 // recalculated and their field states are set to 52 // COMPUTED. isSet() must return true. 53 int y = cal.get(YEAR); 54 if (!(cal.isSet(MONTH) && cal.isSet(DAY_OF_MONTH))) { 55 throw new RuntimeException("After get(): MONTH field=" + cal.isSet(MONTH) 56 + ", DAY_OF_MONTH field=" + cal.isSet(DAY_OF_MONTH)); 57 } 58 } 59 }