< prev index next >

test/java/text/Format/DateFormat/WeekDateTest.java

Print this page
rev 8819 : 8202088: Japanese new era implementation
8207152: Placeholder for Japanese new era should be two characters
8206120: Add test cases for lenient Japanese era parsing
Reviewed-by: coffeys, naoto
Contributed-by: deepak.kejriwal@oracle.com
   1 /*
   2  * Copyright (c) 2010, 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  */


 120     // Non-lenient mode test
 121     private static void nonLenientTest(String[] data) {
 122         ywdFormat.setLenient(false);
 123         for (String date : data) {
 124             try {
 125                 Date d = ywdFormat.parse(date);
 126                 throw new RuntimeException("No ParseException thrown with " + date);
 127             } catch (ParseException e) {
 128                 // OK
 129             }
 130         }
 131         ywdFormat.setLenient(true);
 132     }
 133 
 134 
 135     private static void noWeekDateSupport() throws Exception {
 136         // Tests with Japanese Imperial Calendar that doesn't support week dates.
 137         Calendar jcal = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
 138                                              new Locale("ja", "JP", "JP"));
 139 







 140         jcal.setFirstDayOfWeek(MONDAY);
 141         jcal.setMinimalDaysInFirstWeek(4);
 142         SimpleDateFormat sdf = new SimpleDateFormat("Y-'W'ww-u");
 143         sdf.setCalendar(jcal);
 144         Date d = sdf.parse("21-W01-3"); // 2008-12-31 == H20-12-31
 145         GregorianCalendar gcal = newCalendar();
 146         gcal.setTime(d);
 147         if (gcal.get(YEAR) != 2008
 148             || gcal.get(MONTH) != DECEMBER
 149             || gcal.get(DAY_OF_MONTH) != 31) {
 150             String s = String.format("noWeekDateSupport: got %04d-%02d-%02d, expected 2008-12-31%n",
 151                                      gcal.get(YEAR),
 152                                      gcal.get(MONTH)+1,
 153                                      gcal.get(DAY_OF_MONTH));

 154             throw new RuntimeException(s);
 155         }
 156     }
 157 
 158     private static GregorianCalendar newCalendar() {
 159         // Use GMT to avoid any surprises related DST transitions.
 160         GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
 161         // Setup the ISO 8601 compatible parameters
 162         cal.setFirstDayOfWeek(MONDAY);
 163         cal.setMinimalDaysInFirstWeek(4);
 164         return cal;
 165     }
 166 }
   1 /*
   2  * Copyright (c) 2010, 2019, 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  */


 120     // Non-lenient mode test
 121     private static void nonLenientTest(String[] data) {
 122         ywdFormat.setLenient(false);
 123         for (String date : data) {
 124             try {
 125                 Date d = ywdFormat.parse(date);
 126                 throw new RuntimeException("No ParseException thrown with " + date);
 127             } catch (ParseException e) {
 128                 // OK
 129             }
 130         }
 131         ywdFormat.setLenient(true);
 132     }
 133 
 134 
 135     private static void noWeekDateSupport() throws Exception {
 136         // Tests with Japanese Imperial Calendar that doesn't support week dates.
 137         Calendar jcal = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
 138                                              new Locale("ja", "JP", "JP"));
 139 
 140         String format = "2-W01-2"; // 2019-12-31 == N1-12-31
 141         int expectedYear = 2019;
 142         // Check the current era, Heisei or NewEra
 143         if (System.currentTimeMillis() < 1556668800000L) {
 144             format = "21-W01-3"; // 2008-12-31 == H20-12-31
 145             expectedYear = 2008;
 146         }
 147         jcal.setFirstDayOfWeek(MONDAY);
 148         jcal.setMinimalDaysInFirstWeek(4);
 149         SimpleDateFormat sdf = new SimpleDateFormat("Y-'W'ww-u");
 150         sdf.setCalendar(jcal);
 151         Date d = sdf.parse(format);
 152         GregorianCalendar gcal = newCalendar();
 153         gcal.setTime(d);
 154         if (gcal.get(YEAR) != expectedYear
 155             || gcal.get(MONTH) != DECEMBER
 156             || gcal.get(DAY_OF_MONTH) != 31) {
 157             String s = String.format("noWeekDateSupport: got %04d-%02d-%02d, expected %4d-12-31%n",
 158                                      gcal.get(YEAR),
 159                                      gcal.get(MONTH)+1,
 160                                      gcal.get(DAY_OF_MONTH),
 161                                      expectedYear);
 162             throw new RuntimeException(s);
 163         }
 164     }
 165 
 166     private static GregorianCalendar newCalendar() {
 167         // Use GMT to avoid any surprises related DST transitions.
 168         GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
 169         // Setup the ISO 8601 compatible parameters
 170         cal.setFirstDayOfWeek(MONDAY);
 171         cal.setMinimalDaysInFirstWeek(4);
 172         return cal;
 173     }
 174 }
< prev index next >