问题
I need to get value from JSpinner.
private JSpinner[] timeSpinner;
private SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date time = new Date();
try {
time = format.parse(timeSpinner[i].getValue().toString());
} catch (ParseException e) {
e.printStackTrace();
}
data[i] = time.toString();
System.out.println(data[i]);
The error message:
java.text.ParseException: Unparseable date: "Thu Jan 01 01:05:00 CET 1970"
回答1:
The SimpleDateFormat you are creating has a different format from the date you are trying to parse:
new SimpleDateFormat("HH:mm:ss");
Thu Jan 01 01:05:00 CET 1970
Have a look at the SimpleDateFormat docs
Edit: Actually looking again it looks like you already have a Date object why not just use that?
time = (Date)timeSpinner[i].getValue();
In order to turn the date you have into a String you just have to do the above to get a Date and then do
String formattedDate = format.format(time);
回答2:
I can't see there any reasons for casting and parsing, you have to read tutorial about JSpinners and to set SpinnerDateModel for JSpinner
instace, then you couldn't needed to solve parsing / casting Date
instance from JSpinner
simple example
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();
}
});
}
}
来源:https://stackoverflow.com/questions/9176698/getting-the-value-from-jspinner-swing