1 /*
2  * Copyright (c) 2012, 2013, 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 7130335 7130335
27  * @summary Make sure that round-trip conversion (format/parse) works
28  *          with old timestamps in Europe/Moscow and with multiple time zone letters.
29  */
30 import java.text.*;
31 import java.util.*;
32 import static java.util.GregorianCalendar.*;
33 
34 public class Bug7130335 {
35     private static final TimeZone MOSCOW = TimeZone.getTimeZone("Europe/Moscow");
36     private static final TimeZone LONDON = TimeZone.getTimeZone("Europe/London");
37     private static final TimeZone LA = TimeZone.getTimeZone("America/Los_Angeles");
38     private static final TimeZone[] ZONES = {
39         MOSCOW, LONDON, LA
40     };
41 
main(String[] args)42     public static void main(String[] args) throws Exception {
43         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z", Locale.US);
44         sdf.setTimeZone(MOSCOW);
45         Calendar cal = new GregorianCalendar(MOSCOW, Locale.US);
46         cal.clear();
47         // Try both +03:00 and +02:00
48         cal.set(1922, SEPTEMBER, 30);
49         test(sdf, cal);
50         cal.add(DAY_OF_YEAR, 1);
51         test(sdf, cal);
52         cal.set(1991, MARCH, 31);
53         // in daylight saving time
54         test(sdf, cal);
55         cal.add(DAY_OF_YEAR, 1);
56         test(sdf, cal);
57         // Try the current timestamp
58         cal.setTimeInMillis(System.currentTimeMillis());
59         test(sdf, cal);
60 
61         // tests for multiple time zone letters (8000529)
62         test8000529("yyyy-MM-dd HH:mm:ss.SSS Z (z)");
63         test8000529("yyyy-MM-dd HH:mm:ss.SSS Z (zzzz)");
64         test8000529("yyyy-MM-dd HH:mm:ss.SSS z (Z)");
65         test8000529("yyyy-MM-dd HH:mm:ss.SSS zzzz (Z)");
66 
67     }
68 
test(SimpleDateFormat sdf, Calendar cal)69     private static void test(SimpleDateFormat sdf, Calendar cal) throws Exception {
70         Date d = cal.getTime();
71         String f = sdf.format(d);
72         System.out.println(f);
73         Date pd = sdf.parse(f);
74         String p = sdf.format(pd);
75         if (!d.equals(pd) || !f.equals(p)) {
76             throw new RuntimeException("format: " + f + ", parse: " + p);
77         }
78     }
79 
test8000529(String fmt)80     private static void test8000529(String fmt) throws Exception {
81         for (TimeZone tz : ZONES) {
82             SimpleDateFormat sdf = new SimpleDateFormat(fmt, Locale.US);
83             sdf.setTimeZone(tz);
84             Calendar cal = new GregorianCalendar(tz, Locale.US);
85             cal.clear();
86             cal.set(2012, JUNE, 22);
87             test(sdf, cal);
88             cal.set(2012, DECEMBER, 22);
89             test(sdf, cal);
90             cal.setTimeInMillis(System.currentTimeMillis());
91             test(sdf, cal);
92         }
93     }
94 }
95