Home » Java programming language

Java Calendar setTimeInMillis() Method with Example

Calendar Class setTimeInMillis() method: Here, we are going to learn about the setTimeInMillis() method of Calendar Class with its syntax and example.
Submitted by Preeti Jain, on February 02, 2020

Calendar Class setTimeInMillis() method

  • setTimeInMillis() method is available in java.util package.
  • setTimeInMillis() method is used to sets the current time from the given parameter (time_in_ms) of this Calendar.
  • setTimeInMillis() method is a non-static method, it is accessible with the class object and if we try to access the method with the class name then we will get an error.
  • setTimeInMillis() method does not throw an exception at the time of set time in UTC milliseconds.

Syntax:

    public void setTimeInMillis(long time_in_ms);

Parameter(s):

  • long time_in_ms – represents the time in milliseconds.

Return value:

The return type of this method is void, it returns nothing.

Example:

// Java Program to demonstrate the example of
// void setTimeInMillis(long time_in_ms) method of Calendar

import java.util.*;

public class SetTimeInMillis {
    public static void main(String args[]) {
        // Instantiating a Calendar object
        Calendar ca = Calendar.getInstance();

        // By using setTime(long) method is to 
        // set the time in milliseconds of the
        // Calendar
        ca.setTimeInMillis(2000);

        //Display Time     
        System.out.println("ca: " + ca.getTime());
    }
}

Output

ca: Thu Jan 01 00:00:02 GMT 1970



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.