Java Utility Library

Java Date - getTime() Method



The java.util.Date.getTime() method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

Syntax

public long getTime()

Parameters

No parameter is required.

Return Value

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.

Exception

NA

Example:

In the example below, the java.util.Date.getTime() method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented till the given Date object.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Date
    Date Dt = new Date();

    //Number of milliseconds since January 1, 1970
    long timediff = Dt.getTime();

    //printing the time difference in milliseconds
    System.out.println(timediff + " milliseconds");
  }
}

The output of the above code will be:

1620471935681 milliseconds

❮ Java.util - Date