Home » Java programming language

Java Date getTime() Method with Example

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

Date Class getTime() method

  • getTime() method is available in java.util package.
  • getTime() method is used to get a date with time in milliseconds.
  • getTime() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getTime() method does not throw an exception at the time of returning date with milliseconds.

Syntax:

    public long getTime();

Parameter(s):

  • It does not accept any parameter.

Return value:

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

Example:

// Java program to demonstrate the example 
// of long getTime() method of Date

import java.util.*;

public class GetTimeOfDate {
    public static void main(String[] args) {
        // create a Date object with dates
        Date this_date = new Date(2016, 8, 20);

        // By using getTime() method is to return 
        // the difference of milliseconds passed from the date

        long time_diff = this_date.getTime();

        // Display time_diff
        System.out.println("this_date.getTime(): " + time_diff);
    }
}

Output

this_date.getTime(): 61432473600000


Comments and Discussions!

Load comments ↻





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