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 
  31 import java.util.*;
  32 
  33 public class bug4401223 extends IntlTest {
  34 
  35     public void Test4401223a() {
  36         int status = 0;
  37         String s = null;
  38 
  39         try {
  40             Date date = new Date(2000-1900, Calendar.FEBRUARY, 29);
  41             GregorianCalendar gc = new GregorianCalendar();
  42             gc.setTime(date);
  43             gc.setLenient(false);
  44             gc.set(Calendar.YEAR, 2001);
  45             s = "02/29/00 & set(YEAR,2001) = " + gc.getTime().toString();
  46         } catch (Exception ex) {
  47             status++;
  48             s = "Exception occurred for 2/29/00 & set(YEAR,2001): " + ex;
  49         }
  50         if (status > 0) {
  51             errln(s);
  52         } else {
  53             logln(s);
  54         }
  55     }
  56 
  57     public void Test4401223b() {
  58         int status = 0;
  59         String s = null;
  60 
  61         try {
  62             Date date = new Date(2000-1900, Calendar.DECEMBER, 31);
  63             GregorianCalendar gc = new GregorianCalendar();
  64             gc.setTime(date);
  65             gc.setLenient(false);
  66             gc.set(Calendar.YEAR, 2001);
  67 
  68             if (gc.get(Calendar.YEAR) != 2001 ||
  69                 gc.get(Calendar.MONTH) != Calendar.DECEMBER ||
  70                 gc.get(Calendar.DATE) != 31 ||
  71                 gc.get(Calendar.DAY_OF_YEAR) != 365) {
  72                 status++;
  73                 s = "Wrong Date : 12/31/00 & set(YEAR,2001) ---> " + gc.getTime().toString();
  74             } else {
  75                 s = "12/31/00 & set(YEAR,2001) = " + gc.getTime().toString();
  76             }
  77         } catch (Exception ex) {
  78             status++;
  79             s = "Exception occurred for 12/31/00 & set(YEAR,2001) : " + ex;
  80         }
  81         if (status > 0) {
  82             errln(s);
  83         } else {
  84             logln(s);
  85         }
  86     }
  87 
  88     public static void main(String[] args) throws Exception {
  89         new bug4401223().run(args);
  90     }
  91 }