In this blog, we will see how to create a calendar which will show the current date using jcalendar in java swing example.
Let’s first look at the Swing component
JCalendar
JCalendar is a Java Swing component designed for selecting dates. It provides a highly configurable user interface and supports various date selection modes.
What Is JCalendar?
JCalendar is a Java Swing component designed to let users select dates on the swing created UI. Its highly configurable, JCalendar supports various date selection modes like single date selection, multiple dates selection and date range selection. JCalendar was built on top of Java’s DateChooserPanel to provide an intuitive user interface when selecting dates.
JCalendar is not a standard component built into the core Java Swing library (javax.swing). Instead, it is a highly popular, external, open-source component that extends Swing’s capabilities by providing a visual, interactive calendar and date-picker interface. It is part of the JCalendar library developed by Kaiser.
The primary goal of the JCalendar library is to simplify date input and selection for Java desktop applications.
Key Components of the JCalendar Library
The JCalendar library provides several classes, each designed for a specific date-related function:
1. JCalendar (The Core Component)
This is the main class representing the entire calendar display.
- Function: Displays a full month view (like a wall calendar) allowing the user to browse months and years.
- Purpose: Often used for static display or within a dedicated panel where the user needs a constant view of the calendar grid.
- Interface: It implements a
PropertyChangeListenermechanism, allowing developers to listen for changes when a user selects a new date.
2. JDateChooser (The Date Picker)
This is the most frequently used component for modern user interfaces.
- Function: Provides a standard text field combined with a button that launches a pop-up calendar (similar to those found on websites).
- Purpose: Ideal for forms where space is limited and the user needs to enter a single date efficiently.
- Advantage: It handles date formatting and parsing automatically, simplifying the process of getting a valid
java.util.Dateobject from the user.
3. JMonthChooser and JYearChooser
These smaller components are used internally by JCalendar and can also be used independently.
- Function: Provides a dropdown list for selecting a month (
JMonthChooser) and spinner controls for selecting a year (JYearChooser). - Purpose: Useful for building custom date range selectors or other date-specific filters.
Implementation Steps (Using JDateChooser)
To use JCalendar, you must first include the necessary library.
Step 1: Include the Library
Since JCalendar is external, you must download the JAR file (typically jcalendar-x.x.x.jar) and add it to your project’s build path. If using Maven, add the dependency:
XML
<dependency>
<groupId>com.toedter</groupId>
<artifactId>jcalendar</artifactId>
<version>1.4</version> </dependency>
Step 2: Basic Code Setup
The following snippet shows how to instantiate and use a JDateChooser within a standard Swing application:
Java
import com.toedter.calendar.JDateChooser;
import javax.swing.*;
import java.awt.*;
import java.util.Date;
public class CalendarExample extends JFrame {
public CalendarExample() {
// 1. Setup the JFrame
setTitle("JCalendar Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
// 2. Instantiate JDateChooser
JDateChooser dateChooser = new JDateChooser();
dateChooser.setPreferredSize(new Dimension(150, 30));
// Optional: Set a default date
dateChooser.setDate(new Date());
// 3. Add to the JFrame
add(new JLabel("Select Date:"));
add(dateChooser);
// 4. Add a button to retrieve the selected date
JButton getButton = new JButton("Get Date");
getButton.addActionListener(e -> {
// Retrieve the selected date as a java.util.Date object
Date selectedDate = dateChooser.getDate();
if (selectedDate != null) {
JOptionPane.showMessageDialog(this,
"Selected Date: " + selectedDate.toString());
} else {
JOptionPane.showMessageDialog(this, "No date selected!");
}
});
add(getButton);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
Key Features and Customization
1. Date Retrieval (The Crucial Step)
The most important method is JDateChooser.getDate(), which returns the selected date as a standard java.util.Date object. This simplifies integration with databases or other Java date/time operations.
2. Formatting
By default, the JDateChooser uses the system’s default locale and date format. You can customize the displayed format using the setDateFormatString() method:
Java
dateChooser.setDateFormatString("yyyy-MM-dd"); // Example: 2025-11-22
3. Date Restrictions
You can restrict the dates a user can select, which is crucial for applications dealing with historical data or future appointments:
- Minimum Date:
dateChooser.setMinSelectableDate(Date minDate) - Maximum Date:
dateChooser.setMaxSelectableDate(Date maxDate)
4. Customizing Look and Feel
Since JCalendar components are based on Swing, they automatically inherit the current Look and Feel (L&F) of the application, ensuring visual consistency with other Swing components.
Code for jcalendar in java swing example
import com.toedter.calendar.JCalendar;
import javax.swing.*;
public class JCalendarSwingExampleSLB {
public static void main(String[] args) {
//create a JFrame object and add the name to it
JFrame frame = new JFrame("JCalendar Swing Example");
//set frame size as 500 * 500
frame.setSize(500, 500);
// create a JCalendar object using new
JCalendar calendar = new JCalendar();
// Now, add this JCalendar object to the frame
frame.add(calendar);
// set frame visibility to true so that it can be visisble
frame.setVisible(true);
// set default close operation on close click
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Code Explaination
Code for JFrame includes JCalendar and JFrame classes as required imports, while also specifying a main method with specific steps for their implementation;
An JFrame object with the specified title and size (500 by 500 pixels) is created. Once created, an instance of JCalendar class is instantiated and added to the frame – its visibility being set to true for optimal viewing results.
When executed, this program will show a window containing a calendar which the user can navigate easily.
Complete Jcalendar code
import com.toedter.calendar.JCalendar;
import com.toedter.calendar.JDateChooser;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class JCalendarOperationsSLB {
public static void main(String[] args) {
JFrame frame = new JFrame("JCalendar Operations");
frame.setSize(500, 500);
frame.setLayout(null);
JCalendar calendar = new JCalendar();
calendar.setBounds(50, 50, 300, 200);
frame.add(calendar);
JLabel label = new JLabel("Selected Date: ");
label.setBounds(50, 270, 150, 25);
frame.add(label);
JDateChooser dateChooser = new JDateChooser();
dateChooser.setBounds(50, 310, 200, 25);
frame.add(dateChooser);
// create a JButton to get the selected date
JButton button1 = new JButton("Get Selected Date");
button1.setBounds(50, 360, 200, 25);
frame.add(button1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Date date = calendar.getDate();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String dateString = sdf.format(date);
label.setText("Selected Date: " + dateString);
}
});
JButton button2 = new JButton("Set to Current Date");
button2.setBounds(270, 360, 200, 25);
frame.add(button2);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calendar.setDate(Calendar.getInstance().getTime());
}
});
JButton button3 = new JButton("Set to Current Date");
button3.setBounds(270, 310, 200, 25);
frame.add(button3);
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dateChooser.setDate(Calendar.getInstance().getTime());
}
});
JButton button4 = new JButton("Clear Selected Date");
button4.setBounds(270, 410, 200, 25);
frame.add(button4);
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calendar.setDate(null);
dateChooser.setDate(null);
label.setText("Selected Date: ");
}
});
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Customizing JCalendar
JCalendar can be used to do a wide range of customization options to tailor its appearance and behavior to your needs. Here are some of the most commonly used customization options:
Changing the Locale
JCalendar uses the default locale to display dates. You can change the locale by calling the setLocale() method:
calendar.setLocale(Locale.US);
Changing the Date Selection Mode
JCalendar supports different date selection modes such as single date, multiple dates, and date range. we can set the date selections mode by calling the setSelectionMode() method:
calendar.setCalendarSelectionMode(CalendarSelectionModel.SINGLE_SELECTION);
JCalendar in Real-World Applications
JCalendar is an indispensable component for numerous Java applications, such as booking systems, event schedulers and project management tools. With its customizable user interface and robust set of features, it makes an excellent choice for applications requiring date selection.
Conclusion
In this blog, we have seen that using jcalendar and swing we can create java application with user interface that can display calendar.
FAQ
JCalendar FAQs
1. What is the licensing for JCalendar? Is it free to use?
JCalendar is open-source and typically released under the GNU Lesser General Public License (LGPL). This means you can use the library freely in commercial applications without having to release your own source code, provided you adhere to the LGPL terms for the JCalendar library itself.
2. How can I get the selected date in a custom format (e.g., “dd-MMM-yyyy”)?
You can directly control the date format displayed in the JDateChooser‘s text field using the setDateFormatString() method.
Java
// Example to set the format to DD-Month-YYYY
dateChooser.setDateFormatString("dd-MMM-yyyy");
However, when you retrieve the date using dateChooser.getDate(), it returns a standard java.util.Date object. To format this object into a string for display or logging, you must use the standard Java class SimpleDateFormat:
Java
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String formattedDate = formatter.format(dateChooser.getDate());
3. Does JCalendar support the modern Java 8 Date/Time API (java.time)?
No, the core JCalendar library components like JDateChooser and JCalendar are based on the older java.util.Date and java.util.Calendar classes, reflecting the time when the library was developed.
If your application uses the modern java.time API (like LocalDate or ZonedDateTime), you must perform a conversion after retrieval:
Java
import java.time.LocalDate;
import java.time.ZoneId;
// 1. Get the old Date object
java.util.Date oldDate = dateChooser.getDate();
// 2. Convert to modern LocalDate
LocalDate localDate = oldDate.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
4. How can I change the language (locale) of the calendar component?
You can set the locale for the entire calendar display when creating the component or later using the setLocale() method. This affects the names of the months and days displayed in the pop-up:
Java
// Set the locale to French
dateChooser.setLocale(new Locale("fr", "FR"));
// If you want the month names to be displayed in Spanish (Spain)
// JCalendar will automatically update its labels.
dateChooser.setLocale(new Locale("es", "ES"));
5. What is the recommended way to select a date range using JCalendar?
The standard JDateChooser only allows selecting a single date. To implement a date range selector, the common pattern is to use two separate JDateChooser components:
- One for the Start Date.
- One for the End Date.
You would then add logic (e.g., using PropertyChangeListener or a simple action listener) to ensure the End Date cannot be earlier than the Start Date using the setMaxSelectableDate() and setMinSelectableDate() methods on the respective components for validation.

For over 15 years, I have worked as a hands-on Java Architect and Senior Engineer, specializing in building and scaling high-performance, enterprise-level applications. My career has been focused primarily within the FinTech, Telecommunications, or E-commerce sector, where I’ve led teams in designing systems that handle millions of transactions per day.
Checkout my profile here : AUTHOR https://simplifiedlearningblog.com/author/