Java Program to display date and time in Milliseconds


Firstly, to display date and time, import the following package.

import java.util.Date;

Display the current date using Date class.

Date currentDate = new Date();

Let us now get milliseconds since Jan. 1, 1970 GMT

long res = currentDate.getTime();

The following is an example.

Example

 Live Demo

import java.util.Date;
public class Example {
   public static void main(String args[]) {
      Date currentDate = new Date();
      System.out.println("Current date = "+currentDate);
      // getting milliseconds since Jan. 1, 1970 GMT
      long res = currentDate.getTime();
      System.out.println("Milliseconds = " + res);
   }
}

Output

Current date = Mon Nov 19 05:32:19 UTC 2018
Milliseconds = 1542605539301

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 27-Jun-2020

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements