< prev index next >

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

Print this page
rev 54350 : 8205432: Replace the placeholder Japanese era name
Reviewed-by: rriggs
   1 /*
   2  * Copyright (c) 2010, 2018, 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);
   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 == R1-12-31
 141         int expectedYear = 2019;
 142         // Check the current era, Heisei or Reiwa
 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);
< prev index next >