How do I create JSpinner component with date value?

The SpinnerDateModel allow us to display and select date information from the JSpinner component. By default, the initial value of the model will be set to the current date. To change it we can call the setValue method of the JSpinner object.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.util.GregorianCalendar;
import java.util.Calendar;

public class JSpinnerDate extends JFrame {
    public JSpinnerDate() {

    public static void main(String[] args) {
                () -> new JSpinnerDate().setVisible(true));

    private void initializeUI() {
        setSize(500, 500);
        setLayout(new BorderLayout());

        // Create a SpinnerDateModel with current date as the initial value.
        SpinnerDateModel model = new SpinnerDateModel();

        // Set the spinner value to October 9, 2021.
        JSpinner spinner = new JSpinner(model);
        Calendar calendar = new GregorianCalendar(2021, Calendar.OCTOBER, 9);

        getContentPane().add(spinner, BorderLayout.NORTH);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.