Set a GregorianCalendar object to a particular date in Java

Java 8Object Oriented ProgrammingProgramming

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

 Live Demo

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
raja
Published on 17-Dec-2018 16:40:59
Advertisements