Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 a GregorianCalendar object to a particular date in Java
To work with the GregorianCalendar class, import the following package.
import java.util.GregorianCalendar;
Firstly, create a GregorianCalendar object.
GregorianCalendar calendar = new GregorianCalendar();
Now, set the above-created object to a date. Here 0 is for 1st month.
calendar.set(2018, 0, 25);
The following is an example.
Example
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;
public class Demo {
public static void main(String[] args) {
GregorianCalendar calendar = new GregorianCalendar();
// 0 is for 1st month
calendar.set(2018, 0, 25);
System.out.println("" + calendar.getTime());
}
}
Output
Thu Jan 25 16:51:24 UTC 2018
Advertisements