JDK-4216582 : (tz) JVM should detect run-time changes to time zone setting in Windows NT
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.1.7,1.2.2,1.3.0
  • Priority: P5
  • Status: Resolved
  • Resolution: Won't Fix
  • OS: windows_nt,windows_xp
  • CPU: x86
  • Submitted: 1999-03-02
  • Updated: 2018-08-17
  • Resolved: 2018-08-17
Related Reports
Duplicate :  
Description
The JVM apparently does not detect system changes to the timezone in NT 4.0.  To
see this, run the test case below and, while the application is running, change the system timezone (In Control Panel, select Date/Time | Timezone).  Then click
the application's "Show TimeZone" button.  The timezone displayed will be the original timezone.

This occurs in 1.1.7B and 1.2.

========================================================
========================================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


public class TestTimeZone extends JFrame {
    
    JTextArea ta = new JTextArea(15, 50);
    JTextField tf = new JTextField(10);
    JButton showTimeButton = new JButton("Show Time");
    JButton setButton  = new JButton("Set Time Zone");
    JPanel buttonPanel = new JPanel(new FlowLayout());
    JPanel fieldsPanel = new JPanel(new GridBagLayout());
    JButton showZoneButton = new JButton("Show TimeZone");
    
    public TestTimeZone() {
        String[] ids = TimeZone.getAvailableIDs();
        System.out.println(ids.length + " time zones");
        for (int i = 0; i < ids.length; i++)
            System.out.print(ids[i] + " ");
        System.out.println("\n\n");
        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        buttonPanel.add(showTimeButton);
        buttonPanel.add(setButton);
        buttonPanel.add(showZoneButton);
        
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weighty = 0;
        ((GridBagLayout)fieldsPanel.getLayout()).setConstraints(tf, gbc);
        fieldsPanel.add(tf);
        gbc.gridy = 1;
        gbc.weighty = 1;
        JScrollPane taScroll = new JScrollPane(ta);
        ((GridBagLayout)fieldsPanel.getLayout()).setConstraints(taScroll, gbc);
        fieldsPanel.add(taScroll);
        c.add(BorderLayout.CENTER, fieldsPanel);
        c.add(BorderLayout.SOUTH, buttonPanel);
        showZoneButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ta.append("   TimeZone.getID says: " + TimeZone.getDefault().getID() + "\n");
                ta.append("   Calendar.getInstance().get(Calendar.ZONE_OFFSET) says: " + Calendar.getInstance().get(Calendar.ZONE_OFFSET) + "\n");
                ta.append("   Calendar.getInstance().get(Calendar.DST_OFFSET) says: " + Calendar.getInstance().get(Calendar.DST_OFFSET) + "\n");
            }});
        showTimeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                ta.append(new Date().toString() + "\n");
            }});
        setButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("TimeZone before = " + TimeZone.getDefault());
                TimeZone.setDefault(TimeZone.getTimeZone(tf.getText()));
                System.out.println("TimeZone after = " + TimeZone.getDefault());
            }});
        pack();
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }});
        //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    
    
    Date date = new Date();
    
    public final static void main(String[] a) {
        new TestTimeZone();
    }
}

Comments
EVALUATION Javadoc needs to be more precise about whether and when the time zone is updated. norbert.lindenberg@Eng 1999-03-03 The Java time and date classes don't track the OS timezone on the fly; they only read it when the VM first starts up. It would be undesirable (from an engineering perspective) to try to support such a feature, since it would involve a performance cost (polling the OS) to support a feature of limited utility, and would break existing code that is written to work with the current behavior. alan.liu@eng 1999-03-08 It's very questionable whether it's Okay for all applications to modify all TimeZone objects at run-time. What we could do is to: - support event/listener for time zone change, and - add a method to enable/disable DST of a TimeZone object Probably we should consider other time related notification mechanism, such as system date/time change. masayoshi.okutsu@Eng 2000-02-15
15-02-2000