Time Functions in Java


Java provides the Date class available in java.util package, this class encapsulates the current date and time. The time functions can be accessed from the java.util.Date class. This represents an instance of time with millisecond precision.

One of the time function in Java is the getTime() function. It returns the number of milliseconds that have passed since January 1, 1970, 00:00:00 GMT. A program that demonstrates this is given as follows −

Example

 Live Demo

import java.util.*;
public class Example {
   public static void main(String[] args) {
      Date d = new Date(95, 7, 15);
      long num = d.getTime();
      System.out.println("From the date 1-1-1970 to the date 15-07-1995, " + num + "       milliseconds have passed.");
   }
}

Output

From the date 1-1-1970 to the date 15-07-1995, 808444800000 milliseconds have passed.

Now let us understand the above program.

First, a date is created. Then the function getTime() is used to find the number of milliseconds that have passed since January 1, 1970, 00:00:00 GMT. Then this is displayed. The code snippet that demonstrates this is given as follows −

Date d = new Date(95, 7, 15);
long num = d.getTime();
System.out.println("From the date 1-1-1970 to the date 15-07-1995, " + num + " milliseconds have passed.");

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

632 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements