Home » Java programming language

Java Calendar getTimeInMillis() Method with Example

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

Calendar Class getTimeInMillis() method

  • getTimeInMillis() method is available in java.util package.
  • getTimeInMillis() method is used to get time in milliseconds of this Calendar.
  • getTimeInMillis() 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.
  • getTimeInMillis() method does not throw an exception at the time of returning the current time of this Calendar.

Syntax:

    public long getTimeInMillis();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is long, it returns this Calendar time in milliseconds.

Example:

// Java Program to demonstrate the example of
// long getTimeInMillis() method of Calendar

import java.util.*;

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

        // By using getTime() method is to display
        // date with time
        System.out.println("ca.getTime(): " + ca.getTime());

        // By using getTimeInMillis() method is to
        // return the date with time in ms
        long time_in_ms = ca.getTimeInMillis();

        //Display Date
        System.out.println("ca.getTimeInMillis(): " + time_in_ms);
    }
}

Output

ca.getTime(): Mon Jan 27 08:09:04 GMT 2020
ca.getTimeInMillis(): 1580112544558


Comments and Discussions!

Load comments ↻





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