1 /*
2  * Copyright (c) 2005, 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 4296930 5033603 7092679
27  * @summary Make sure that Java runtime detects the platform time zone
28  * correctly. Also make sure that the system time zone detection code
29  * detects the "Automatically adjust clock for daylight saving
30  * changes" setting correctly on Windows.
31  * @run applet/manual=yesno DefaultTimeZoneTest.html
32  */
33 
34 import javax.swing.*;
35 import java.awt.*;
36 import java.awt.event.*;
37 import java.text.*;
38 import java.util.*;
39 
40 public class DefaultTimeZoneTest extends JApplet implements Runnable {
41     static final String FORMAT = "yyyy-MM-dd HH:mm:ss zzzz (XXX)";
42     JLabel tzid;
43     JLabel label;
44     SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
45     JButton button = new JButton("English");
46     Thread clock;
47     boolean english = false;
48 
49     @Override
init()50     public void init() {
51         tzid = new JLabel("Time zone ID: " + sdf.getTimeZone().getID(), SwingConstants.CENTER);
52         tzid.setAlignmentX(Component.CENTER_ALIGNMENT);
53         label = new JLabel(sdf.format(new Date()), SwingConstants.CENTER);
54         label.setAlignmentX(Component.CENTER_ALIGNMENT);
55         button.addActionListener(new ActionListener() {
56                 @Override
57                 @SuppressWarnings("deprecation")
58                 public void actionPerformed(ActionEvent e) {
59                     english = (english == false);
60                     Locale loc = english ? Locale.US : Locale.getDefault();
61                     sdf = new SimpleDateFormat(FORMAT, loc);
62                     button.setLabel(!english ? "English" : "Local");
63                 }
64             });
65         button.setAlignmentX(Component.CENTER_ALIGNMENT);
66         JPanel panel = new JPanel();
67         panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
68         panel.add(Box.createRigidArea(new Dimension(0, 10)));
69         panel.add(tzid);
70         panel.add(Box.createRigidArea(new Dimension(0, 5)));
71         panel.add(label);
72         panel.add(Box.createRigidArea(new Dimension(0, 10)));
73         panel.add(button);
74         getContentPane().add(panel);
75     }
76 
77     @Override
start()78     public void start() {
79         clock = new Thread(this);
80         clock.start();
81     }
82 
83     @Override
stop()84     public void stop() {
85         clock = null;
86     }
87 
88     @Override
run()89     public void run() {
90         Thread me = Thread.currentThread();
91 
92         while (clock == me) {
93             // Reset the default time zone so that
94             // TimeZone.getDefault will detect the platform time zone
95             TimeZone.setDefault(null);
96             System.setProperty("user.timezone", "");
97             TimeZone tz = TimeZone.getDefault();
98             sdf.setTimeZone(tz);
99             tzid.setText("Time zone ID: " + tz.getID());
100             label.setText(sdf.format(new Date()));
101             repaint();
102             try {
103                 Thread.sleep(1000);
104             } catch (InterruptedException e) {
105             }
106         }
107     }
108 }
109