1/* 2 * Copyright (c) 2001, 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 4401223 27 * @summary Make sure that GregorianCalendar doesn't cause IllegalArgumentException at some special situations which are related to the Leap Year. 28 * @library /java/text/testlib 29 */ 30 31import java.util.Date; 32import java.util.GregorianCalendar; 33 34import static java.util.GregorianCalendar.*; 35 36public class bug4401223 extends IntlTest { 37 38 public void Test4401223a() { 39 int status = 0; 40 String s = null; 41 42 try { 43 @SuppressWarnings("deprecation") 44 Date date = new Date(2000 - 1900, FEBRUARY, 29); 45 GregorianCalendar gc = new GregorianCalendar(); 46 gc.setTime(date); 47 gc.setLenient(false); 48 gc.set(YEAR, 2001); 49 s = "02/29/00 & set(YEAR,2001) = " + gc.getTime().toString(); 50 } catch (Exception ex) { 51 status++; 52 s = "Exception occurred for 2/29/00 & set(YEAR,2001): " + ex; 53 } 54 if (status > 0) { 55 errln(s); 56 } else { 57 logln(s); 58 } 59 } 60 61 public void Test4401223b() { 62 int status = 0; 63 String s = null; 64 65 try { 66 @SuppressWarnings("deprecation") 67 Date date = new Date(2000 - 1900, DECEMBER, 31); 68 GregorianCalendar gc = new GregorianCalendar(); 69 gc.setTime(date); 70 gc.setLenient(false); 71 gc.set(YEAR, 2001); 72 73 if (gc.get(YEAR) != 2001 74 || gc.get(MONTH) != DECEMBER 75 || gc.get(DATE) != 31 76 || gc.get(DAY_OF_YEAR) != 365) { 77 status++; 78 s = "Wrong Date : 12/31/00 & set(YEAR,2001) ---> " + gc.getTime().toString(); 79 } else { 80 s = "12/31/00 & set(YEAR,2001) = " + gc.getTime().toString(); 81 } 82 } catch (Exception ex) { 83 status++; 84 s = "Exception occurred for 12/31/00 & set(YEAR,2001) : " + ex; 85 } 86 if (status > 0) { 87 errln(s); 88 } else { 89 logln(s); 90 } 91 } 92 93 public static void main(String[] args) throws Exception { 94 new bug4401223().run(args); 95 } 96} 97