1 /*
 2  * Copyright (c) 2000, 2023, 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 import java.io.*;
25 import java.util.*;
26 import java.text.*;
27 
28 import org.junit.jupiter.api.Test;
29 
30 import static org.junit.jupiter.api.Assertions.fail;
31 
32 /**
33  * @test
34  * @bug 4358730
35  * @summary test that confirms Zero-Padding on year.
36  * @run junit bug4358730
37  */
38 
39 public class bug4358730 {
40 
41     String[] patterns = {"y",    "yy", "yyy",  "yyyy", "yyyyy"};
42     String[][] data = {
43         /* 2 A.D. */    {"2",    "02", "002",  "0002", "00002"},
44         /* 20 A.D. */   {"20",   "20", "020",  "0020", "00020"},
45         /* 200 A.D. */  {"200",  "00", "200",  "0200", "00200"},
46         /* 2000 A.D. */ {"2000", "00", "2000", "2000", "02000"},
47     };
48     int[] year = {2, 20, 200, 2000};
49 
50     int datasize = data.length;
51     int nPatterns = data[0].length;
52 
53     @Test
54     public void Test4358730() {
55         TimeZone saveZone = TimeZone.getDefault();
56         Locale saveLocale = Locale.getDefault();
57 
58         try {
59             TimeZone.setDefault(TimeZone.getTimeZone("PST"));
60             Locale.setDefault(Locale.US);
61             SimpleDateFormat sdf = new SimpleDateFormat();
62 
63             for (int i = 0; i < datasize; i++) {
64                 @SuppressWarnings("deprecation")
65                 Date d = new Date(year[i]-1900, 10, 15);
66                 for (int j = 0; j < nPatterns; j++) {
67                     sdf.applyPattern(patterns[j]);
68                     if (!data[i][j].equals(sdf.format(d))) {
69                         fail("Invalid format : " + sdf.format(d) +
70                             ", expected : " + data[i][j]);
71                     }
72                 }
73             }
74         }
75         finally {
76             TimeZone.setDefault(saveZone);
77             Locale.setDefault(saveLocale);
78         }
79     }
80 }