Path: blob/master/test/jdk/java/text/Format/DateFormat/WeekDateTest.java
41152 views
/*1* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 426745026* @summary Unit test for week date support27*/2829import java.text.*;30import java.util.*;31import static java.util.GregorianCalendar.*;3233public class WeekDateTest {34static SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);35static SimpleDateFormat ywdFormat = new SimpleDateFormat("YYYY-'W'ww-u", Locale.US);36static {37ymdFormat.setCalendar(newCalendar());38ywdFormat.setCalendar(newCalendar());39}4041// Round-trip Data42static final String[][] roundTripData = {43{ "2005-01-01", "2004-W53-6" },44{ "2005-01-02", "2004-W53-7" },45{ "2005-12-31", "2005-W52-6" },46{ "2007-01-01", "2007-W01-1" },47{ "2007-12-30", "2007-W52-7" },48{ "2007-12-31", "2008-W01-1" },49{ "2008-01-01", "2008-W01-2" },50{ "2008-12-29", "2009-W01-1" },51{ "2008-12-31", "2009-W01-3" },52{ "2009-01-01", "2009-W01-4" },53{ "2009-12-31", "2009-W53-4" },54{ "2010-01-03", "2009-W53-7" },55{ "2009-12-31", "2009-W53-4" },56{ "2010-01-01", "2009-W53-5" },57{ "2010-01-02", "2009-W53-6" },58{ "2010-01-03", "2009-W53-7" },59{ "2008-12-28", "2008-W52-7" },60{ "2008-12-29", "2009-W01-1" },61{ "2008-12-30", "2009-W01-2" },62{ "2008-12-31", "2009-W01-3" },63{ "2009-01-01", "2009-W01-4" },64{ "2009-01-01", "2009-W01-4" },65};6667// Data for leniency test68static final String[][] leniencyData = {69{ "2008-12-28", "2009-W01-0" },70{ "2010-01-04", "2009-W53-8" },71{ "2008-12-29", "2008-W53-1" },72};7374static final String[] invalidData = {75"2010-W00-1",76"2010-W55-1",77"2010-W03-0",78"2010-W04-8",79"2010-W04-19"80};8182public static void main(String[] args) throws Exception {83formatTest(roundTripData);84parseTest(roundTripData);85parseTest(leniencyData);86nonLenientTest(invalidData);87noWeekDateSupport();88}8990private static void formatTest(String[][] data) throws Exception {91for (String[] dates : data) {92String regularDate = dates[0];93String weekDate = dates[1];94Date date = null;95date = ymdFormat.parse(regularDate);96String s = ywdFormat.format(date);97if (!s.equals(weekDate)) {98throw new RuntimeException("format: got="+s+", expecetd="+weekDate);99}100}101}102103private static void parseTest(String[][] data) throws Exception {104for (String[] dates : data) {105String regularDate = dates[0];106String weekDate = dates[1];107Date date1 = null, date2 = null;108date1 = ymdFormat.parse(regularDate);109date2 = ywdFormat.parse(weekDate);110if (!date1.equals(date2)) {111System.err.println(regularDate + ": date1 = " + date1);112System.err.println(weekDate + ": date2 = " + date2);113throw new RuntimeException("parse: date1 != date2");114}115}116}117118119// Non-lenient mode test120private static void nonLenientTest(String[] data) {121ywdFormat.setLenient(false);122for (String date : data) {123try {124Date d = ywdFormat.parse(date);125throw new RuntimeException("No ParseException thrown with " + date);126} catch (ParseException e) {127// OK128}129}130ywdFormat.setLenient(true);131}132133134private static void noWeekDateSupport() throws Exception {135// Tests with Japanese Imperial Calendar that doesn't support week dates.136Calendar jcal = Calendar.getInstance(TimeZone.getTimeZone("GMT"),137new Locale("ja", "JP", "JP"));138139String format = "2-W01-2"; // 2019-12-31 == R1-12-31140int expectedYear = 2019;141// Check the current era, Heisei or Reiwa142if (System.currentTimeMillis() < 1556668800000L) {143format = "21-W01-3"; // 2008-12-31 == H20-12-31144expectedYear = 2008;145}146jcal.setFirstDayOfWeek(MONDAY);147jcal.setMinimalDaysInFirstWeek(4);148SimpleDateFormat sdf = new SimpleDateFormat("Y-'W'ww-u");149sdf.setCalendar(jcal);150Date d = sdf.parse(format);151GregorianCalendar gcal = newCalendar();152gcal.setTime(d);153if (gcal.get(YEAR) != expectedYear154|| gcal.get(MONTH) != DECEMBER155|| gcal.get(DAY_OF_MONTH) != 31) {156String s = String.format("noWeekDateSupport: got %04d-%02d-%02d, expected %4d-12-31%n",157gcal.get(YEAR),158gcal.get(MONTH)+1,159gcal.get(DAY_OF_MONTH),160expectedYear);161throw new RuntimeException(s);162}163}164165private static GregorianCalendar newCalendar() {166// Use GMT to avoid any surprises related DST transitions.167GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));168// Setup the ISO 8601 compatible parameters169cal.setFirstDayOfWeek(MONDAY);170cal.setMinimalDaysInFirstWeek(4);171return cal;172}173}174175176