author | amurillo |
Wed, 15 Aug 2012 16:49:38 -0700 | |
changeset 13465 | d3fc5d192448 |
parent 5506 | 202f599c92aa |
child 40948 | 5869c625afaa |
permissions | -rw-r--r-- |
3098
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
1 |
/* |
5506 | 2 |
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. |
3098
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
4 |
* |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
8 |
* |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
13 |
* accompanied this code). |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
14 |
* |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
18 |
* |
5506 | 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. |
|
3098
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
22 |
*/ |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
23 |
|
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
24 |
/** |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
25 |
* @test |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
26 |
* @bug 6609750 |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
27 |
* @summary Make sure that SimpleDateFormat.format() formats years correctly. |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
28 |
*/ |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
29 |
import java.text.*; |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
30 |
import java.util.*; |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
31 |
|
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
32 |
public class Bug6609750 { |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
33 |
|
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
34 |
public static void main(String[] args) { |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
35 |
boolean error = false; |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
36 |
|
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
37 |
Locale defaultLocale = Locale.getDefault(); |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
38 |
Locale.setDefault(Locale.US); |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
39 |
|
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
40 |
Date[] dates = { |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
41 |
new Date(9-1900, Calendar.JUNE, 12), |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
42 |
new Date(99-1900, Calendar.JUNE, 12), |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
43 |
new Date(999-1900, Calendar.JUNE, 12), |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
44 |
new Date(2009-1900, Calendar.JUNE, 12), |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
45 |
new Date(30009-1900, Calendar.JUNE, 12), |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
46 |
}; |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
47 |
|
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
48 |
String[] patterns = { |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
49 |
"y", "yy", "yyy", "yyyy", "yyyyy", "yyyyyy" |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
50 |
}; |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
51 |
String[][] expectedResults = { |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
52 |
{"9", "09", "009", "0009", "00009", "000009"}, |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
53 |
{"99", "99", "099", "0099", "00099", "000099"}, |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
54 |
{"999", "99", "999", "0999", "00999", "000999"}, |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
55 |
{"2009", "09", "2009", "2009", "02009", "002009"}, |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
56 |
{"30009", "09", "30009", "30009", "30009", "030009"}, |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
57 |
}; |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
58 |
|
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
59 |
SimpleDateFormat sdf = new SimpleDateFormat(); |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
60 |
for (int dateNo = 0; dateNo < dates.length; dateNo++) { |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
61 |
Date date = dates[dateNo]; |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
62 |
for (int patternNo = 0; patternNo < patterns.length; patternNo++) { |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
63 |
sdf.applyPattern(patterns[patternNo]); |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
64 |
String got = sdf.format(date); |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
65 |
if (!expectedResults[dateNo][patternNo].equals(got)) { |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
66 |
error = true; |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
67 |
System.err.println("Failed: Unexpected format result: " + |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
68 |
"Expected: \"" + expectedResults[dateNo][patternNo] + |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
69 |
"\", Got: \"" + got + "\" for date " + date + |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
70 |
" with pattern \"" + patterns[patternNo] + "\""); |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
71 |
} |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
72 |
} |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
73 |
} |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
74 |
|
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
75 |
Locale.setDefault(defaultLocale); |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
76 |
if (error) { |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
77 |
throw new RuntimeException("SimpleDateFormat.format() error."); |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
78 |
}; |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
79 |
} |
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
80 |
|
2b04258e7975
6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents:
diff
changeset
|
81 |
} |