1 /* 2 * Copyright (c) 2009, 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 Date[] dates = { 41 new Date(9-1900, Calendar.JUNE, 12), 42 new Date(99-1900, Calendar.JUNE, 12), 43 new Date(999-1900, Calendar.JUNE, 12), 44 new Date(2009-1900, Calendar.JUNE, 12), 45 new Date(30009-1900, Calendar.JUNE, 12), 46 }; 47 48 String[] patterns = { 49 "y", "yy", "yyy", "yyyy", "yyyyy", "yyyyyy" 50 }; 51 String[][] 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 }; 58 59 SimpleDateFormat sdf = new SimpleDateFormat(); 60 for (int dateNo = 0; dateNo < dates.length; dateNo++) { 61 Date date = dates[dateNo]; 62 for (int patternNo = 0; patternNo < patterns.length; patternNo++) { 63 sdf.applyPattern(patterns[patternNo]); 64 String got = sdf.format(date); 65 if (!expectedResults[dateNo][patternNo].equals(got)) { 66 error = true; 67 System.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 } 74 75 Locale.setDefault(defaultLocale); 76 if (error) { 77 throw new RuntimeException("SimpleDateFormat.format() error."); 78 }; 79 } 80 81 }