Java.getTime() Method

Overview
getTime() method of Java Date Class present in java.util package returns the number of milliseconds since Jan 1 of the year 1970, 00:00:00 GTM (Greenwich Mean Time) which is performed by creating the Date object. This method can be used to assign a date and by using java.getTime() method we can return the number of milliseconds since Jan 1, 1970.
Syntax of Java.getTime()
Parameters of Java.getTime()
This method does not accept any parameters.
Return Value of Java.getTime()
This method returns the number of milliseconds since January 1, 1970, 00:00:00 GTM (Unix Epoch).
Exceptions of Java.getTime()
This method does not throw any Runtime Exceptions.
Example
Below is an example of java.getTime() method from Date class: The below given java program implements the java.getTime() method which is present in Date class, we have tentatively assumed the Date to be .
Output:
Explanation Using the java.getTime() method from Date class we have obtained a number of milliseconds from Jan 1, 1970 to the given date i.e, March 20, 2001, is 60945868800000 milliseconds.
What is java.getTime()?
java.getTime() method of Java Date Class in util package in Java returns the number of milliseconds from Jan 1, 1970, in form of 00:00:00 GTM. This also returns the present date by creating the Date object using the getTime() method. This method does not accept any parameters and does not throw any Runtime Exceptions.
More Examples:
Some more examples of java.getTime() method using getInstance() method of Calendar class in Java. Using the getInstance() method we can set a date up to which the number of milliseconds has to be counted since January 1, 1970. This is clearly shown in the below implementation:
Output:
Explanation Using the java.getTime() method from Date class we have obtained number of milliseconds since Jan 1, 1970, to the given date i.e, November 1, 2001, is 1004577411672 milliseconds.
The second example for a better understanding of the getTime() method in Java where the Date considered is October 12, 2020 is:
Output:
Explanation Using the java.getTime() method from the Date class we have obtained a number of milliseconds from Jan 1, 1970 to the given date i.e, October 12, 2020, is 1602465375847 milliseconds.
Conclusion:
- java.getTime() method is from the Date class which is present in the util package.
- This method does not accept any parameters.
- This method does not throw any Runtime Exceptions.
- This method returns the number of milliseconds since Jan 1, 1970.