- 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
Advertisements