3

I have a jspinner in the format yyyy-MM-dd hh:mm:ss. The problem is the spinner 'hh' only goes up to value '12'. How can i make it so it goes up to 24 hours?

3 Answers 3

12

you have define for two things for JSpinner (basically contents of the tutorial fully answering your question)

for 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();
            }
        });
    }
}
1
  • Note that there's an initialization bug related to such spinners, described here. If you add a component to this example that snatches initial focus from the spinner, timezone formatting will be applied only after the spinner gains focus.
    – predi
    Sep 21, 2016 at 11:08
6

The JSpinner.DateEditor component uses the same formatting as the SimpleDateFormat.

Have a look at the SimpleDateFormat formats.

Your format string should be :

yyyy-MM-dd HH:mm:ss
1
  • 1
    To be explicit… Uppercase/Lowercase matters on date-time formatters, for both java.text.SimpleDateFormat as well as for Joda-Time. HH is for 24-hour clock, hh is for 12-hour clock. Jan 19, 2014 at 23:45
1

ITS SIMPLE TRY IT.......

Date datenow = Calendar.getInstance().getTime();
SpinnerDateModel smb = new SpinnerDateModel(datenow, null, null, Calendar.HOUR_OF_DAY);
SPIN_DATE.setModel(smb);
JSpinner.DateEditor d = new JSpinner.DateEditor(SPIN_DATE, "dd-MMM-yyyy");
SPIN_DATE.setEditor(d);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.