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