Saturday, October 11, 2025
HomeLanguagesJavaCalendar setTimeInMillis() Method in Java with Examples

Calendar setTimeInMillis() Method in Java with Examples

The setTimeInMillis(long mill_sec) method in Calendar class is used to set Calendars time represented by this Calendar from the passed long value.

Syntax:

public void setTimeInMillis(long mill_sec)

Parameters: The method takes one parameter mill_sec of long datat-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 setTimeInMillis() Method of Calendar class:
Example 1:




// Java code to illustrate
// setTimeInMillis() method
  
import java.util.*;
  
public class Calendar_Demo {
    public static void main(String args[])
    {
  
        // Creating calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Getting the time in milliseconds
        System.out.println("The Current"
                           + " Time is: "
                           + calndr.getTime());
  
        // Changing time to 2000 milli-second
        calndr.setTimeInMillis(2000);
  
        // Displaying the new time
        System.out.println("After setting"
                           + " Time: "
                           + calndr.getTime());
    }
}


Output:

The Current Time is: Fri Feb 22 08:00:54 UTC 2019
After setting Time: Thu Jan 01 00:00:02 UTC 1970

Example 2:




// Java code to illustrate
// setTimeInMillis() method
  
import java.util.*;
  
public class Calendar_Demo {
    public static void main(String args[])
    {
  
        // Creating calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Getting the time in milliseconds
        System.out.println("The Current"
                           + " Time is: "
                           + calndr.getTime());
  
        // Changing time to 8000 milli-second
        calndr.setTimeInMillis(8000);
  
        // Displaying the new time
        System.out.println("After setting"
                           + " Time: "
                           + calndr.getTime());
    }
}


Output:

The Current Time is: Fri Feb 22 08:01:02 UTC 2019
After setting Time: Thu Jan 01 00:00:08 UTC 1970

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

RELATED ARTICLES

Most Popular

Dominic
32352 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11884 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7103 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS