I'm not exactly 100% sure what you're trying to acheive, but take a look at the example below, maybe it will help you out. I use a button to set the date, then another button that pops up a JOptionPane
that shows the previous date saved from the first button click.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class TimeSpinner {
Date date;
JButton setDate = new JButton("Set Date");
JButton showSpinner = new JButton("Show Spinner");
JTextField jtfDate = new JTextField(15);
JSpinner spinner = new JSpinner();
JPanel panel;
public TimeSpinner() {
jtfDate.setEditable(false);
panel = new JPanel(new BorderLayout());
panel.add(setDate, BorderLayout.CENTER);
panel.add(jtfDate, BorderLayout.NORTH);
panel.add(showSpinner, BorderLayout.SOUTH);
setDate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
date = new Date();
jtfDate.setText(date.toString());
}
});
showSpinner.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (date != null) {
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String dateString = formatter.format(date);
spinner.setModel(new SpinnerListModel(new String[]{dateString}));
JOptionPane.showConfirmDialog(null, spinner, "Spinner:"
,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
}
}
});
JFrame frame = new JFrame("Date Spinner Demo");
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new TimeSpinner();
}
});
}
}
With the following code, I was able to achieve this using the a the SpinnerModel
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String dateString = formatter.format(date);
spinner.setModel(new SpinnerListModel(new String[]{dateString}));
EDIT
For different formatting, just change the format. If you want in time
- For hours in 24 hour mode
"HH:mm:ss"
- For 12 hour mode
"hh:mm:ss a"