我有一个有日期的Jspinner,可以选择日期。但是,jspinner中的日期应该显示一段时间过去的实际时间,例如1分钟后。
我尝试使用while循环或if。
如何更改?
package datetest;
import java.awt.BorderLayout;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class DateTest {
JFrame f;
JPanel p;
JSpinner sp;
SpinnerDateModel dateModel;
Date dates;
Date dates1;
DateFormat df;
public static void main(String[] args) {
new DateTest().startApp();
}
private void startApp() {
f = new JFrame();
p = new JPanel();
dates = new Date();
dateModel = new SpinnerDateModel(dates,null,null,0);
sp = new JSpinner(dateModel);
sp.setEditor(new JSpinner.DateEditor(sp, "HH:mm dd-MM-yyyy"));
sp.addChangeListener(new CalendarListener());
p.add(sp);
f = new JFrame();
f.getContentPane().add(p, BorderLayout.CENTER);
f.setSize(450,550);
f.setResizable(false);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class CalendarListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
JSpinner jSpinner = (JSpinner) e.getSource();
dates1 = (Date) jSpinner.getValue();
df = new SimpleDateFormat("HH:mm dd-MM-yyyy");
String nt = df.format(dates1);
System.out.println(dates1);
}
}
}