How to Create a Date Object using the Calendar Class in Java Last Updated : 08 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, the Calendar class can provide a flexible way to handle the dates and times. This article demonstrates how to create the Date object using the Calendar class by setting the specific date and time and converting it to the Date object and printing the exact date and time. This approach offers more control and flexibility compared to the Date class.Steps to Create a Date Object using the Calendar classWe can import the necessary classes like Calendar and Date classes from the java.util package.Create the Calendar Instance of the Calendar class using the Calendar.getInstance().We can set the desired year using the set method of Calendar class to set the year.We can set the desired year and month using the set method.Now, We can convert the Calendar Instance to the Date Object.Finally, print the Date Object.Program to Create a Date Object using the Calendar classBelow is the Program to Create a Date Object using Calendar Class: Java // Java Program to Create a Date Object // Using the Calendar Class import java.util.Calendar; import java.util.Date; // Driver Class public class DateExample { // Main Function public static void main(String[] args) { // Create a Calendar instance Calendar calendar = Calendar.getInstance(); // Set the desired date and time calendar.set(Calendar.YEAR, 2024); // Note: Months are zero-based in Calendar class calendar.set(Calendar.MONTH, Calendar.JUNE); calendar.set(Calendar.DAY_OF_MONTH, 17); calendar.set(Calendar.HOUR_OF_DAY, 10); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 0); // Convert Calendar to Date Date date = calendar.getTime(); // Print the Date System.out.println("The date is: " + date); } } OutputThe date is: Mon Jun 17 10:30:00 UTC 2024 Explanation of above Code:Calendar calendar = Calendar.getInstance(): Create the Calendar object with the current date and time.calendar.set(Calendar.YEAR, 2024): It can be set the year to the 2024.calendar.set(Calendar.MONTH, Calendar.JUNE): Set the month to the June.calendar.set(Calendar.DAY_OF_MONTH, 17): Sets the day of the month to 17.calendar.set(Calendar.HOUR_OF_DAY, 10): Sets the hour of day to 10AM.calendar.set(Calendar.MINUTE, 30): Sets the minute to 30.calendar.set(Calendar.SECOND, 0): Sets the second to 0.Date date = calendar.getTime(): It can be converts the Calendar object to the Date object.This program can demonstrates how to use the Calendar class to set the specific date and time, convert it to the Date object and then print it. Comment More infoAdvertise with us Next Article How to Add Time to Date in Java? K kadambalamatclo Follow Improve Article Tags : Java Java Programs Java-Date-Time Practice Tags : Java Similar Reads Java Program to Display Dates of a Calendar Year in Different Format As different countries do opt for different formats. So here the goal is simply to print dates of calendar in different years. The generic symbolic notation opted across the globe is: Symbolic NotationDepictsyyearMmonth in year dday in month Eday of week Concept: Whenever it comes down to the date a 4 min read How to convert Date to String in Java Given a date, the task is to write a Java program to convert the given date into a string. Examples: Input: date = "2020-07-27" Output: 2020-07-27 Input: date = "2018-02-17" Output: 2018-02-17 Method 1: Using DateFormat.format() method Approach: Get the date to be converted.Create an instance of Sim 3 min read How to get Day, Month and Year from Date in Java Given a date in the form of a string, the task is to write a Java Program to get the day, month, and year from the given date. Examples: Input: date = "2020-07-18"Output:Day: 18Month: JulyYear: 2020Explanation: The given date is '2020-07-18', so the day is: 18, the month is: July, and the year is: 2 4 min read How to Add Time to Date in Java? In Java, You can add the time to a Date object and it can be part of the java.util.Calendar package and another approach be used to newer java.time package. I can explain both approaches adding the time to a Date in Java programming. In this article, we are going to learn, how to add time to date in 3 min read How to Convert a String to a LocalDate in Java? Converting a String to a LocalDate in Java is a common operation when you are dealing with date inputs from users. Java provides the LocalDate class in the java.time package for representing a date without time information. LocalDate class is part of the java.time package introduced in Java 8. Strin 2 min read Java Program to Generate Calendar of Any Year Without calendar.get() Function Java program for generating the calendar of any desired year and month let us first go through an illustration before landing upon logic and procedural part. Illustration: Say the user wants to get the calendar of April 2011. Then, he is required to enter the year along with the month as integers an 4 min read Like