All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Get Current Time In Java

Last Updated : Jul 1, 2023

How To Get Current Time In Java

In this article we will show you the solution of how to get current time in java, to collect current time, you need to import time package. In case you are using eclipse then each program imports usual package itself at the first line by default.

To achieve target result you need to use now() constructor of localtime object in java. It returns current locale server time that values printed at terminal as result.

Step By Step Guide On How To Get Current Time In Java :-

We named main class as ‘Curt_prgm’, there we provide definition for public static main method.

As foremost step you have to create LocalTime object with now() constructor to obtain current time.

It is retrieved from your local server or machine which stored on particular variable to print as result at the terminal at lastly.

//Current Time
package java_prgms;
import java.time.LocalTime;
class Curt_prgm
 public static void main(String args[])
   System.out.println("Current Time : "+t);
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. First displaying package will inserted by eclipse to support java codes, if you use eclipse platform for code programs and each time it is imported after successful creation of java project. Then LocalTime package you have to manually import as secondary to get desired result if not you get error.
  3. The main class name is ‘Curt_prgm’, ensure whether you are saved with this name for this program. If same you do not get confuse at any case during execution otherwise at execution comment you have to give file names carefully to get output. At eclipse you get result simply when you are click on run button because here commend lines not necessary.
  4. In main class firstly created main method then we created LocalTime object with now() constructor because it will return current time to object ‘t’.
  5. Now you have current time of result in variable ‘t’, so printed result at terminal with output text ‘Current Time’. It prints complete format of time which means printing time as(hour:minutes:seconds).
  6. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  7. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to successfully obtain local current time in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac’.

After successful compilation you need to execute ‘java Curt_prgm‘ line for gets the output.

When you need to run the program simply click on run button on eclipse result will display at terminal block as ‘Current Time 10:46:23’.

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

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪