Thursday, September 25, 2025
HomeLanguagesJavaCalendar setTime() Method in Java with Examples

Calendar setTime() Method in Java with Examples

The setTime(Date dt) method in Calendar class is used to set Calendars time represented by this Calendar’s time value, with the given or passed date as a parameter.

Syntax:

public final void setTime(Date dt))

Parameters: The method takes one parameter dt of Date type and refers to the given date that is to be set.

Return Value: The method does not return any value.

Below programs illustrate the working of setTime() Method of Calendar class:
Example 1:




// Java code to illustrate
// setTime() method
  
import java.util.*;
public class Calendar_Demo
    extends GregorianCalendar {
    public static void main(String[] args)
    {
        // Creating calendar objects
        Calendar calndr1
            = (Calendar)Calendar.getInstance();
        Calendar calndr2
            = (Calendar)Calendar.getInstance();
  
        // Displaying the current date
        System.out.println("The Current"
                           + " System Date: "
                           + calndr1.getTime());
  
        // Setting to a different date
        calndr1.set(Calendar.MONTH, 5);
        calndr1.set(Calendar.YEAR, 2006);
        calndr1.set(Calendar.DAY_OF_WEEK, 15);
        Date dt = calndr1.getTime();
  
        // Setting the timevalue
        calndr2.setTime(dt);
  
        // Displaying the new date
        System.out.println("The modified"
                           + " Date:"
                           + calndr2.getTime());
    }
}


Output:

The Current System Date: Fri Feb 22 07:33:13 UTC 2019
The modified Date:Sun Jun 18 07:33:13 UTC 2006

Example 2:




// Java code to illustrate
// setTime() method
  
import java.util.*;
  
public class Calendar_Demo
    extends GregorianCalendar {
  
    public static void main(String[] args)
    {
        // Creating calendar objects
        Calendar calndr1
            = (Calendar)Calendar.getInstance();
        Calendar calndr2
            = (Calendar)Calendar.getInstance();
  
        // Displaying the current date
        System.out.println("The Current"
                           + " System Date: "
                           + calndr1.getTime());
  
        // Setting to a different date
        calndr1.set(Calendar.MONTH, 10);
        calndr1.set(Calendar.YEAR, 1995);
        calndr1.set(Calendar.DAY_OF_WEEK, 20);
        Date dt = calndr1.getTime();
  
        // Setting the timevalue
        calndr2.setTime(dt);
  
        // Displaying the new date
        System.out.println("The modified"
                           + " Date: "
                           + calndr2.getTime());
    }
}


Output:

The Current System Date: Fri Feb 22 07:33:20 UTC 2019
The modified Date: Fri Nov 24 07:33:20 UTC 1995

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#setTime(java.util.Date)

RELATED ARTICLES

Most Popular

Dominic
32319 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6682 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6753 POSTS0 COMMENTS
Umr Jansen
6761 POSTS0 COMMENTS