- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a Date object using the Calendar class in Java
For using Calendar class, import the following package.
import java.util.Calendar;
Now, let us create an object of Calendar class.
Calendar calendar = Calendar.getInstance();
Set the date, month and year.
calendar.set(Calendar.YEAR, 2018); calendar.set(Calendar.MONTH, 11); calendar.set(Calendar.DATE, 18);
Create a Date object using Calendar class.
java.util.Date dt = calendar.getTime();
The following is an example.
Example
import java.util.Calendar; public class Demo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); // Set year, month and date calendar.set(Calendar.YEAR, 2018); calendar.set(Calendar.MONTH, 11); calendar.set(Calendar.DATE, 18); // util date object java.util.Date dt = calendar.getTime(); System.out.println("Date: "+dt); } }
Output
Date: Tue Dec 18 08:29:51 UTC 2018
- Related Articles
- Increment a Date using the Java Calendar Class
- Java Program to decrement a Date using the Calendar Class
- Increment a Month using the Calendar Class in Java
- Decrement a Month using the Calendar Class in Java
- Create a Date Picker Calendar in Tkinter
- Compare date time using after() method of Java Calendar
- Compare date time using before() method of Java Calendar
- Java Program to add days to current date using Java Calendar
- How to create date object in Java?
- Set the Date and Time with Gregorian Calendar in Java
- Create a queue using LinkedList class in Java
- How to create an object from class in Java?
- Date Class in Java
- How to create Date object from String value in Java?
- Can we create an object for the abstract class in java?

Advertisements