Java Utility Library

Java Calendar - setTime() Method



The java.util.Calendar.setTime() method is used to set this Calendar's time with the given Date.

Syntax

public final void setTime(Date date)

Parameters

date Specify the date.

Return Value

void type.

Exception

NA

Example:

In the example below, the java.util.Calendar.setTime() method is used to set the given Calendar's time with specified date.

import java.util.*;

public class MyClass {
  public static void main(String[] args){
    //creating a Calendar instance
    Calendar Cal = Calendar.getInstance();

    //Creating a date object from calendar
    //and printing it
    System.out.println("The Calendar is: " + Cal.getTime());

    //sets the Calendar's time with specified date.
    GregorianCalendar cal1 = new GregorianCalendar(1995, 10, 25);
    Date Dt = cal1.getTime();
    Cal.setTime(Dt);

    //Creating a date object from modified
    //calendar and printing it
    System.out.println("Modified Calendar is: " + Cal.getTime());    
  }
}

The output of the above code will be:

The Calendar is: Wed Sep 02 04:45:38 GMT 2020
Modified Calendar is: Sat Nov 25 00:00:00 GMT 1995

❮ Java.util - Calendar