请教JVM如何动态更新时区的问题

09-05-20 sml2002
问题描述:

当系统改了时区后,JVM无法动态获取到已更改的时区,但更改系统时间System.currentTimeMillis()还是可以获取到修改过的毫秒数,不知谁知道动态修改时区的方法(不重启JVM的情况下)

测试代码如下:运行后,修改系统时区,夏令时也可以,修改后再点击show time 按钮,发现UTC时间与系统时区均没有变化,如果修改了系统时间,则UTC时间是要变化的,谁知道在我修改了时区后不重启JVM就能够正确同步时区的方法,谢谢大家。

import java.awt.BorderLayout;   
import java.awt.GridLayout;   
import java.awt.event.ActionEvent;   
import java.awt.event.ActionListener;   
import java.util.Calendar;   
import java.util.Date;   
import java.util.GregorianCalendar;   
  
import javax.swing.JButton;   
import javax.swing.JFrame;   
import javax.swing.JLabel;   
import javax.swing.JPanel;   
import javax.swing.JScrollPane;   
import javax.swing.JTextArea;   
import javax.swing.SwingConstants;   
  
public class TimeZoneTest extends JFrame {   
  
    private void init() {   
  
        setLocation(LOCATION_X,LOCATION_Y);   
        setTitle("TimeTest");   
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);   
  
        JPanel northPanel = new JPanel();   
        northPanel.setLayout(new GridLayout());   
        northPanel.add(new JLabel("Time: ", SwingConstants.LEFT));   
  
        add(northPanel, BorderLayout.NORTH);   
  
        final JTextArea textArea = new JTextArea(10, 10);   
        JScrollPane scrollPane = new JScrollPane(textArea);   
  
        add(scrollPane, BorderLayout.CENTER);   
  
        JPanel southPanel = new JPanel();   
  
        JButton showButton = new JButton("Show Time");   
        southPanel.add(showButton);   
        showButton.addActionListener(new ActionListener() {   
            @Override  
            public void actionPerformed(ActionEvent e) {   
                textArea.setText(testTime());   
            }   
        });   
  
        add(southPanel, BorderLayout.SOUTH);   
  
  
    }   
  
    private String testTime() {   
  
        StringBuilder strTime = new StringBuilder();   
  
        long curTime = System.currentTimeMillis();   
  
        Date curDate = new Date(curTime);   
  
        strTime.append("\n current time : " + curDate);   
  
        Calendar ca = new GregorianCalendar();   
  
        curDate = new Date(ca.getTimeInMillis());   
  
        strTime.append("\n the Calender time is : " + curDate);   
  
        strTime.append("\n the time zone : "  
                + ca.getTimeZone().getID());   
  
        return strTime.toString();   
    }   
  
    public static void main(String[] args) {   
  
        TimeZoneTest tzt = new TimeZoneTest();   
  
        tzt.init();   
  
        tzt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  
        tzt.setVisible(true);   
  
    }   
  
    public static final int DEFAULT_WIDTH = 320;   
    public static final int DEFAULT_HEIGHT = 300;   
  
    public static final int LOCATION_Y = 200;   
    public static final int LOCATION_X = 300;   
       
    private static final long serialVersionUID = 1L;   
}  

猜你喜欢