Java Swing Date

Sheeraz Gul Oct 12, 2023
  1. Show the Current Date Using Swing in Java
  2. Create a Date Picker Using Swing in Java
Java Swing Date

Showing or choosing a date in Java can be done with the Swing package. This tutorial demonstrates how to work with dates in Java Swing.

Show the Current Date Using Swing in Java

We can show the current date in Java using the Swing package. To create the GUI, we can implement any class from Java Swing.

We use the PropertyChangeListener class to show the current date in Java Swing. See the example below.

package delftstack;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Show_Date implements PropertyChangeListener {
  public static void main(String[] args) {
    Show_Date DateShow = new Show_Date();
    createWindow(DateShow);
  }

  private static void createWindow(Show_Date DateShow) {
    JFrame J_Frame = new JFrame("Show Date");
    J_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    createUI(J_Frame, DateShow);
    J_Frame.setSize(300, 200);
    J_Frame.setLocationRelativeTo(null);
    J_Frame.setVisible(true);
  }

  private static void createUI(final JFrame J_Frame, Show_Date DateShow) {
    JPanel J_Panel = new JPanel();
    LayoutManager Layout_Manager = new GridLayout(6, 2);
    J_Panel.setLayout(Layout_Manager);
    J_Panel.setSize(200, 200);
    J_Panel.setBorder(BorderFactory.createTitledBorder("Date Format"));

    DateFormat Date_Format = new SimpleDateFormat("dd MMM YYYY");
    JFormattedTextField Today_Date = new JFormattedTextField(Date_Format);
    Today_Date.setName("Today");
    Today_Date.setColumns(10);
    Today_Date.setEditable(false);
    JLabel Today_Label = new JLabel("Date:");
    Today_Label.setLabelFor(Today_Date);
    Today_Date.setValue(new Date());

    J_Panel.add(Today_Label);
    J_Panel.add(Today_Date);
    JPanel Main_Panel = new JPanel();
    Main_Panel.add(J_Panel);

    J_Frame.getContentPane().add(Main_Panel, BorderLayout.CENTER);
  }

  @Override
  public void propertyChange(PropertyChangeEvent evt) {
    // TODO Auto-generated method stub
  }
}

The code above will show the current date in a frame. See output:

Show Current Date Using Swing

Create a Date Picker Using Swing in Java

We can create a Date Picker class using the Java Swing package from which we select a date from a calendar. Date Picker will pop up a calendar, and we can select any date from it.

The code below demonstrates how to create and display a date picker and select dates from the date picker. See example:

package delftstack;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class DatePick {
  int DATE_MONTH = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
  int DATE_YEAR = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
  ;
  JLabel J_Label = new JLabel("", JLabel.CENTER);
  String DATE_DAY = "";
  JDialog J_Dialog;
  JButton[] J_Button = new JButton[49];

  public DatePick(JFrame J_Frame_Parent) {
    J_Dialog = new JDialog();
    J_Dialog.setModal(true);
    String[] Header = {
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    JPanel J_Panel1 = new JPanel(new GridLayout(7, 7));
    J_Panel1.setPreferredSize(new Dimension(700, 120));

    for (int i = 0; i < J_Button.length; i++) {
      final int selection = i;
      J_Button[i] = new JButton();
      J_Button[i].setFocusPainted(false);
      J_Button[i].setBackground(Color.white);
      if (i > 6)
        J_Button[i].addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            DATE_DAY = J_Button[selection].getActionCommand();
            J_Dialog.dispose();
          }
        });
      if (i < 7) {
        J_Button[i].setText(Header[i]);
        J_Button[i].setForeground(Color.red);
      }
      J_Panel1.add(J_Button[i]);
    }
    JPanel J_Panel2 = new JPanel(new GridLayout(1, 3));
    JButton Previous_Button = new JButton("<< Previous");
    Previous_Button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        DATE_MONTH--;
        Display_Date();
      }
    });
    J_Panel2.add(Previous_Button);
    J_Panel2.add(J_Label);
    JButton Next_Button = new JButton("Next >>");
    Next_Button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        DATE_MONTH++;
        Display_Date();
      }
    });
    J_Panel2.add(Next_Button);
    J_Dialog.add(J_Panel1, BorderLayout.CENTER);
    J_Dialog.add(J_Panel2, BorderLayout.SOUTH);
    J_Dialog.pack();
    J_Dialog.setLocationRelativeTo(J_Frame_Parent);
    Display_Date();
    J_Dialog.setVisible(true);
  }

  public void Display_Date() {
    for (int i = 7; i < J_Button.length; i++) J_Button[i].setText("");
    java.text.SimpleDateFormat Simple_Date_Format = new java.text.SimpleDateFormat("MMMM yyyy");
    java.util.Calendar Calendar = java.util.Calendar.getInstance();
    Calendar.set(DATE_YEAR, DATE_MONTH, 1);
    int Day_Of_Week = Calendar.get(java.util.Calendar.DAY_OF_WEEK);
    int Days_In_Month = Calendar.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
    for (int i = 6 + Day_Of_Week, Day = 1; Day <= Days_In_Month; i++, Day++)
      J_Button[i].setText("" + Day);
    J_Label.setText(Simple_Date_Format.format(Calendar.getTime()));
    J_Dialog.setTitle("Date Picker");
  }

  public String Set_Picked_Date() {
    if (DATE_DAY.equals(""))
      return DATE_DAY;
    java.text.SimpleDateFormat Simple_Date_Format = new java.text.SimpleDateFormat("dd-MM-yyyy");
    java.util.Calendar Calendar = java.util.Calendar.getInstance();
    Calendar.set(DATE_YEAR, DATE_MONTH, Integer.parseInt(DATE_DAY));
    return Simple_Date_Format.format(Calendar.getTime());
  }
}

public class Date_Picker {
  public static void main(String[] args) {
    JLabel J_Label = new JLabel("Date Selected:");
    final JTextField J_Text_Field = new JTextField(20);
    JButton J_Button = new JButton("Choose the Date");
    JPanel J_Panel = new JPanel();
    J_Panel.add(J_Label);
    J_Panel.add(J_Text_Field);
    J_Panel.add(J_Button);
    final JFrame J_Frame = new JFrame();
    J_Frame.getContentPane().add(J_Panel);
    J_Frame.pack();
    J_Frame.setVisible(true);
    J_Button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        J_Text_Field.setText(new DatePick(J_Frame).Set_Picked_Date());
      }
    });
  }
}

The code above will create a date picker in Java and show the selected date. See output:

Swing Date Picker

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Swing