currentTimeMillis() Function In Java

Hi coders! wondering how we can use the current time of the system in our program, i.e to print it on the screen or on a webpage. Java provides this feature through the System class where the function is currentTimeMillis(), which returns the time in milliseconds,  elapsed since midnight, January 1, 1970, GMT.

This time is known as the UNIX epoch. The epoch is the point when time starts, and 1970 was the year when the UNIX operating system was formally introduced.

This function is called using System.currentTimeMillis(). This function returns the current system time in milliseconds. We can use this function to print the current time in hours: minutes: seconds format also. For that, we have to define a long type variable because here we are dealing with the data range of 10ˆ12.

If you want to print this time in HH: MM: SS format. Here is very simple and basic code for the same. Just apply the basic formulas in this way

  1. Minute= Second/60
  2. Hour=Minutes/60

Java program example to show how currentTimeMillis() function works

public class sytem_time {

  public static void main(String[] args) {
  System.out.println("System time in millisecond ="+ System.currentTimeMillis());
  long time=System.currentTimeMillis();
  long totalSeconds = time/1000;
  long currentSecond = totalSeconds % 60;
  long totalMinutes =  totalSeconds/60;
  long currentMinutes =totalMinutes%60 -30 ; 
  long totalHour= totalMinutes/60;

  long currentHour =  totalHour % 24 - 6  ;
  System.out.print("current time in format (HH:MM:SS) = ");




currentTimeMillis() Function In Java

There are also other methods to print system time as well as the date. Java provides lots of classes and function for the same.

You may read:

Leave a Reply

Your email address will not be published. Required fields are marked *