Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/TimeZone/DefaultTimeZoneTest.java
38821 views
/*1* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4296930 5033603 709267926* @summary Make sure that Java runtime detects the platform time zone27* correctly. Also make sure that the system time zone detection code28* detects the "Automatically adjust clock for daylight saving29* changes" setting correctly on Windows.30* @run applet/manual=yesno DefaultTimeZoneTest.html31*/3233import javax.swing.*;34import java.awt.*;35import java.awt.event.*;36import java.text.*;37import java.util.*;3839public class DefaultTimeZoneTest extends JApplet implements Runnable {40static final String FORMAT = "yyyy-MM-dd HH:mm:ss zzzz (XXX)";41JLabel tzid;42JLabel label;43SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);44JButton button = new JButton("English");45Thread clock;46boolean english = false;4748@Override49public void init() {50tzid = new JLabel("Time zone ID: " + sdf.getTimeZone().getID(), SwingConstants.CENTER);51tzid.setAlignmentX(Component.CENTER_ALIGNMENT);52label = new JLabel(sdf.format(new Date()), SwingConstants.CENTER);53label.setAlignmentX(Component.CENTER_ALIGNMENT);54button.addActionListener(new ActionListener() {55@Override56@SuppressWarnings("deprecation")57public void actionPerformed(ActionEvent e) {58english = (english == false);59Locale loc = english ? Locale.US : Locale.getDefault();60sdf = new SimpleDateFormat(FORMAT, loc);61button.setLabel(!english ? "English" : "Local");62}63});64button.setAlignmentX(Component.CENTER_ALIGNMENT);65JPanel panel = new JPanel();66panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));67panel.add(Box.createRigidArea(new Dimension(0, 10)));68panel.add(tzid);69panel.add(Box.createRigidArea(new Dimension(0, 5)));70panel.add(label);71panel.add(Box.createRigidArea(new Dimension(0, 10)));72panel.add(button);73getContentPane().add(panel);74}7576@Override77public void start() {78clock = new Thread(this);79clock.start();80}8182@Override83public void stop() {84clock = null;85}8687@Override88public void run() {89Thread me = Thread.currentThread();9091while (clock == me) {92// Reset the default time zone so that93// TimeZone.getDefault will detect the platform time zone94TimeZone.setDefault(null);95System.setProperty("user.timezone", "");96TimeZone tz = TimeZone.getDefault();97sdf.setTimeZone(tz);98tzid.setText("Time zone ID: " + tz.getID());99label.setText(sdf.format(new Date()));100repaint();101try {102Thread.sleep(1000);103} catch (InterruptedException e) {104}105}106}107}108109110