Java中的JSpinner.DateEditor在初始化时不尊重TimeZone [英] JSpinner.DateEditor in Java Not Respecting TimeZone on Initialization

查看:122
本文介绍了Java中的JSpinner.DateEditor在初始化时不尊重TimeZone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用正在使用java日期的Swing应用程序和Swing的JSpinner作为DateEditor的bug修复。我试图让编辑器默认使用UTC来显示时间,而不是我们的本地时区。该应用程序在Windows上运行,使用Java 8。

I'm working on bugfix to an existing Swing Application that is using java dates and the Swing's JSpinner as a DateEditor. I'm trying to get the Editor to default to using UTC to display the time, rather than our local timezone. The application is running on Windows, using Java 8.

我正在使用的代码如下。

The code I am using is below.

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;

    public class Test {

    public static void main(String [] args) {
        // Initialize some sample dates
        Date now = new Date(System.currentTimeMillis());


        JSpinner spinner = new JSpinner();

        // Create model with a current date and no start/end date boundaries, and set it to the spinner
        spinner.setModel(new SpinnerDateModel(now, null, null, Calendar.MINUTE));

        // Create new date editor with a date format string that also displays the timezone (z)
        // Set the format's timezone to be UTC, and finally set the editor to the spinner
        JSpinner.DateEditor startTimeEditor = new JSpinner.DateEditor(spinner, "yyyy-MMM-dd HH:mm zzz");

        startTimeEditor.getFormat().setTimeZone(TimeZone.getTimeZone("UTC"));
        spinner.setEditor(startTimeEditor);

        JPanel panel = new JPanel();
        panel.add(spinner);
        JOptionPane.showConfirmDialog(null, panel);
    }

}

然而,这段代码有初始化问题。当对话框首次出现时,时间显示在我们当地的时区,而不是UTC。一旦用户首先通过点击与现场进行交互,它将切换到UTC,并从那里正常工作。

This code, however, has an initialization issue. When the Dialog first appears, the time is shown in our local timezone, not UTC. Once the user first interacts with the field by clicking on it, it switches to UTC and works properly from there on out.

如何让字段最初在UTC时间显示?

How can I get the field to display initially in UTC time?

推荐答案

有趣的bug适用于我的解决方法是将微调器的初始值设置为类似于 new Date(0)(即1970年1月1日)的初始值,然后调整编辑器后,调用 spinner.setValue(new Date())

Interesting bug. A workaround that works for me is to set the spinner’s initial value to something like new Date(0) (which is January 1 1970), then after adjusting the editor, call spinner.setValue(new Date()).

真正的问题是Spinner似乎没有更新其文本响应编辑器属性的更改。事实上,JSpinner文档表明编辑器属性根本不是绑定属性。所以另一个解决方法是强制Spinner在更改编辑器时更新:

The real problem is that the Spinner doesn’t seem to update its text in response to a change in the editor property. In fact, the JSpinner documentation suggests that the editor property is not a bound property at all. So another workaround is to force the Spinner to update whenever the editor is changed:

SpinnerModel model = new SpinnerDateModel(now, null, null, Calendar.MINUTE);
JSpinner spinner = new JSpinner(model) {
    @Override
    public void setEditor(JComponent editor) {
        super.setEditor(editor);
        fireStateChanged();
    }
};

这篇关于Java中的JSpinner.DateEditor在初始化时不尊重TimeZone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆