Path: blob/master/test/jdk/java/text/Format/DateFormat/Bug6609750.java
41152 views
/*1* Copyright (c) 2009, 2016, 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 660975026* @summary Make sure that SimpleDateFormat.format() formats years correctly.27*/28import java.text.*;29import java.util.*;3031public class Bug6609750 {3233public static void main(String[] args) {34boolean error = false;3536Locale defaultLocale = Locale.getDefault();37Locale.setDefault(Locale.US);3839@SuppressWarnings("deprecation")40Date[] dates = {41new Date(9-1900, Calendar.JUNE, 12),42new Date(99-1900, Calendar.JUNE, 12),43new Date(999-1900, Calendar.JUNE, 12),44new Date(2009-1900, Calendar.JUNE, 12),45new Date(30009-1900, Calendar.JUNE, 12),46};4748String[] patterns = {49"y", "yy", "yyy", "yyyy", "yyyyy", "yyyyyy"50};51String[][] expectedResults = {52{"9", "09", "009", "0009", "00009", "000009"},53{"99", "99", "099", "0099", "00099", "000099"},54{"999", "99", "999", "0999", "00999", "000999"},55{"2009", "09", "2009", "2009", "02009", "002009"},56{"30009", "09", "30009", "30009", "30009", "030009"},57};5859SimpleDateFormat sdf = new SimpleDateFormat();60for (int dateNo = 0; dateNo < dates.length; dateNo++) {61Date date = dates[dateNo];62for (int patternNo = 0; patternNo < patterns.length; patternNo++) {63sdf.applyPattern(patterns[patternNo]);64String got = sdf.format(date);65if (!expectedResults[dateNo][patternNo].equals(got)) {66error = true;67System.err.println("Failed: Unexpected format result: " +68"Expected: \"" + expectedResults[dateNo][patternNo] +69"\", Got: \"" + got + "\" for date " + date +70" with pattern \"" + patterns[patternNo] + "\"");71}72}73}7475Locale.setDefault(defaultLocale);76if (error) {77throw new RuntimeException("SimpleDateFormat.format() error.");78};79}8081}828384