Get current time in Java

This small example shows how to get the current time in Java.

Since Java 8 there’s a new Date and time API which enables us to use this new LocalTime class. With the LocalTime class we can get the time from any timezone but if we want to use the default, which the server is set to, we can simply call the now() method to get the current time.
The now() method is a static one so we don’t need to create an instance of LocalTime to be able to call it. To print out the current time we use the format() method of the String class to set the format of the output. In this case it’s hours followed by a colon, followed by minutes. We use %d in the string to be formatted which means a number parameter will be inserted at that particular spot, since both getHours() and getMinute() methods return an int.

The output is the current hours and minutes separated by colon. Like:

Search for more Java info on this site here:
Custom Search

Any thoughts or questions?