- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Set the Date and Time with Gregorian Calendar in Java
To work with the GregorianCalendar class, import the following package.
import java.util.GregorianCalendar;
Create a Date object.
Date d = new Date();
Now, create an object and set the time using the setTime() method.
GregorianCalendar cal = new GregorianCalendar(); cal.setTime(d);
The following is an example.
Example
import java.util.Date; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { Date d = new Date(); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(d); System.out.println(d); } }
Output
Mon Nov 19 16:11:31 UTC 2018
- Related Articles
- Gregorian Calendar in Java
- Specify the TimeZone explicitly for Gregorian Calendar in Java
- Determine day of week in month from Gregorian Calendar in Java
- Compare date time using after() method of Java Calendar
- Compare date time using before() method of Java Calendar
- Increment a Date using the Java Calendar Class
- Get time in milliseconds using Java Calendar
- Create a Date object using the Calendar class in Java
- Display nanoseconds with Java Date and Time Conversion Character
- Displaymilliseconds since the epoch with Java Date and Time Conversion Character
- Set Date patterns with SimpleDateFormat in Java
- Display entire date time with milliseconds in Java
- Java Program to decrement a Date using the Calendar Class
- How to set default date time as system date time in MySQL?
- Set current date and time to timestamp in MySQL

Advertisements