How to Create a Date Object using the Calendar Class in Java Last Updated : 08 Jul, 2024 Comments Improve Suggest changes 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 info K kadambalamatclo Follow Improve Article Tags : Java Java Programs Java-Date-Time Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like