我遇到标准JSpinner.DateEditor的问题(可能与其他人一样)。当它与Java.util.Date类一起使用时,它仅从微调器上设置的Locale获取默认格式。它似乎是TimeZone无知的。
如果我们的日期是3月时钟更改日,而英国时间没有2点 - 时间从01:59:59.999 AM到03:00 AM跳跃,节省了一天的光线。
因此,在JSpinner.DateEditor中,我不希望用户在02:00到02:59:59.999之间看到任何内容。当小时为1并且他们点击时,我想跳到3,反之亦然。
是否可以实施任何类型的解决方法来执行此操作?
非常感谢
Andez
答案 0 :(得分:4)
是否可以编辑您的问题并使用此SSCCE来证明您有关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();
}
});
}
}
答案 1 :(得分:1)
在阅读mKorbels解决方案后,我用它来获得我想要的东西。我已将代码修改为以下内容:
package datesandtimes.javax.swing;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class JSpinnerProblem
extends JFrame {
static {
TimeZone simpleTimeZone = new SimpleTimeZone(0, "Elexon Date Time");
TimeZone.setDefault(simpleTimeZone);
}
private static final String DATE_FORMAT_TIMEZONE = "yyyy-MM-dd HH:mm:ss zzz";
private static final TimeZone tz = TimeZone.getTimeZone("Europe/London");
private JPanel panel;
private JPanel panelButtons;
private JSpinner spinner;
private SpinnerDateModel model;
private SimpleDateFormat format;
private JButton buttonClose;
public JSpinnerProblem() {
try {
setDefaultCloseOperation(JSpinnerProblem.EXIT_ON_CLOSE);
setLayout(new BorderLayout(5, 5));
// create instances
panel = new JPanel(new BorderLayout());
panelButtons = new JPanel(new GridLayout(1, 1));
model = new SpinnerDateModel();
buttonClose = new JButton("Close");
spinner = new JSpinner(model);
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
}
});
buttonClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// set date format
format = ((JSpinner.DateEditor) spinner.getEditor()).getFormat();
format.setTimeZone(tz);
format.applyPattern("yyyy-MM-dd HH:mm:ss zzz");
// default to march clock change
Date d = format.parse("2012-03-25 00:00:00 GMT");
model.setValue(d);
panel.add(spinner, BorderLayout.CENTER);
panelButtons.add(buttonClose);
add(panel, BorderLayout.CENTER);
add(panelButtons, BorderLayout.SOUTH);
pack();
} catch (ParseException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JSpinnerProblem().setVisible(true);
}
});
}
}
现在使用JSpinner时,单击向上和向下按钮实际上会根据时区将小时更改为正确的小时。