All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java Get Current Time In Milliseconds

Last Updated : Mar 11, 2024

Java Get Current Time In Milliseconds

In this article we will show you the solution of java get current time in milliseconds, Java's System.currentTimeMillis() method, you will receive the current time in milliseconds. An operating system determines the level of granularity of milliseconds; the unit of time is millisecond.

The time in many operating systems is measured in milliseconds, for instance.

The current time is obtained by getting the current time and then by using the methods getTime() and getTimeInMillis(), both of which return the calendar time and date time in milliseconds.

Using ZonedDateTime in Java 8 is a simple way to get time in milliseconds; I have shown that in the program as a third method.

We will now discuss the idea of how to get current time in milliseconds in java with an example.

Step By Step Guide On Java Get Current Time In Milliseconds :-

import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class TC Example {
   public static void main(String[] args)
   {
 SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
 String dateString = "22-06-2023 11:18:32";
 try{
    Date date = sdf.parse(dateString);
    System.out.println("Given Time in milliseconds : "+date.getTime());
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    System.out.println("Given Time in milliseconds : "+calendar.getTimeInMillis());
 }catch(ParseException e){
    e.printStackTrace();
  }
   }
}
  1. This class provides methods for handling date-related operations and is derived from java.util.Calendar.
  2. A date is a time-based object represented by the class java.util.Date.
  3. The SimpleDateFormat class provides methods for formatting and parsing date and time strings within Java code.
  4. An entry point for the program is defined as the main method.
  5. This method creates an object of the SimpleDateFormat class with a specified pattern (dd-M-yyyy hh:mm:ss).
  6. A String variable is initialized with a date and time value according to the specified pattern.
  7. An exception can be handled during the execution of the program when a try-catch block is used to wrap the code block. In this case, we are handling a possible ParseException, which occurs when we try to parse a date string that is not in the specified pattern.
  8. Using SimpleDateFormat's parse method to convert the input date string into a Date object.
  9. Printing the time in milliseconds by calling the getTime method on the resulting Date object.
  10. Creating a Calendar object using the getInstance method of the Calendar class. According to the timezone and locale settings of the user's computer, this returns a Calendar instance.
  11. Using the setTime method, we can set the date and time of the Calendar object. By setting the date and time to the specified values, the Calendar object will represent the specified date and time in milliseconds.
  12. Called the getTimeInMillis method of the resulting Calendar object to display the time in milliseconds. Using this method, you can get the internal millisecond representation of the date and time of the Calendar object.

Conclusion :-

As a result, we have successfully learned how to get current time in milliseconds in java with an example.

The purpose of this article is to demonstrate how to convert time into milliseconds in Java.

I hope this article on java get current time in milliseconds helps you and the steps and method mentioned above are easy to follow and implement.