Java Utility Library

Java Calendar - setTimeInMillis() Method



The java.util.Calendar.setTimeInMillis() method is used to set this Calendar's current time from the given long value.

Syntax

public void setTimeInMillis(long millis)

Parameters

millis Specify the new time in UTC milliseconds from the epoch.

Return Value

void type.

Exception

NA

Example:

In the example below, the java.util.Calendar.setTimeInMillis() method is used to set the given Calendar's time from the specified milliseconds after January 1 1970.

import java.util.*;

public class MyClass {
  public static void main(String[] args){
    //creating a Calendar object with specified date
    Calendar Cal = new GregorianCalendar(2015, 1, 25);

    //printing the Calendar
    System.out.println("The Calendar is: " + Cal.getTime());

    // set time to 3600 seconds after january 1 1970
    Cal.setTimeInMillis(3600000);

    //printing the Calendar
    System.out.println("Modified Calendar is: " + Cal.getTime());    
  }
}

The output of the above code will be:

The Calendar is: Wed Feb 25 00:00:00 UTC 2015
Modified Calendar is: Thu Jan 01 01:00:00 UTC 1970

❮ Java.util - Calendar