Java JSpinner.DateEditor与TimeZone三月时钟更改一起使用 [英] Java JSpinner.DateEditor working with TimeZone March clock change

查看:140
本文介绍了Java JSpinner.DateEditor与TimeZone三月时钟更改一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对标准JSpinner.DateEditor有问题(可能其他所有人也有).当它与Java.util.Date类一起使用时,它仅从微调器上的区域设置"中获取默认格式.它似乎对TimeZone不了解.

I have a problem with the standard JSpinner.DateEditor (as probably does everyone else). When it works with the Java.util.Date class, it only gets a default format from the Locale set on the spinner. It appears to be TimeZone ignorant.

当我们的日期是3月时钟更改日(在英国没有2点)时,时间将从01:59:59.999 AM跳到03:00 AM,并应用了夏时制.

When we have a Date that is on the March Clock change day which in the UK has no 2 o'clock - the time jumps from 01:59:59.999 AM to 03:00 AM with day light savings applied.

因此,在JSpinner.DateEditor中,我不希望用户看到02:00到02:59:59.999的任何内容.当小时为1时,他们单击向上,我想跳到3,反之亦然.

Therefore in the JSpinner.DateEditor I do not want the user to see anything for 02:00 to 02:59:59.999. When the hour is on 1 and they click up, I want to jump to 3 and vice versa.

是否可以实施任何解决方法来做到这一点?

Is it possible to implement any kind of workaround to do this?

非常感谢

安德兹(Andez)

推荐答案

能否请您编辑问题并使用此 SSCCE 证明您有关DTS的问题

can you please edit your question and use this SSCCE that demonstrating your issue about DTS

import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

public class TimeZoneSpinners {

    private final String[] zones = {"Asia/Tokyo", "Asia/Hong_Kong",
        "Asia/Calcutta", "Europe/Paris", "Europe/London",
        "America/New_York", "America/Los_Angeles"
    };
    private final JLabel[] labels = new JLabel[zones.length];
    private final SimpleDateFormat[] formats = new SimpleDateFormat[zones.length];
    private JSpinner spinner;
    private SpinnerDateModel model;
    private SimpleDateFormat format;
    private JPanel panel;
    private JFrame frame = new JFrame();

    public void makeUI() {
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        model = new SpinnerDateModel();
        model.setValue(date);
        spinner = new JSpinner(model);
        spinner.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                Date date = (Date) ((JSpinner) e.getSource()).getValue();
                for (int i = 0; i < labels.length; i++) {
                    labels[i].setText(formats[i].format(date));
                }
            }
        });
        format = ((JSpinner.DateEditor) spinner.getEditor()).getFormat();
        format.setTimeZone(TimeZone.getTimeZone(zones[0]));
        format.applyPattern("yyyy-MM-dd HH:mm:ss");
        panel = new JPanel(new GridLayout(zones.length, 2, 10, 10));
        for (int i = 0; i < zones.length; i++) {
            formats[i] = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
            formats[i].setTimeZone(TimeZone.getTimeZone(zones[i]));
            JLabel label = new JLabel(zones[i]);
            labels[i] = new JLabel(formats[i].format(date));
            panel.add(label);
            panel.add(labels[i]);
        }
        frame.setLayout(new BorderLayout(10, 10));
        frame.add(spinner, BorderLayout.NORTH);
        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TimeZoneSpinners().makeUI();
            }
        });
    }
}

这篇关于Java JSpinner.DateEditor与TimeZone三月时钟更改一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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