Javaexercise.com

How To Get Current Local Time In Java?

To get the current time in Java, you can use the now() method of the LocalTime class in Java 8 and higher versions.

The now() method returns the current time from the system clock in the default time zone. It means by default it returns your system's current time.

Java 8 DateTime API provides several overloaded versions of the now() method to get the current time accordingly such as:

static LocalTime now()

This method returns the current time from the system clock in the default time zone. So if you are in the US then you will get the current time based on the US time zone.

static LocalTime now(Clock clock)

This method returns the current time from the specified clock. You can use this to get the current universal Time (UTC).

static LocalTime now(ZoneId zone)

This method returns the current time from the system clock in the specified time zone. For example, you can use this to get the current time of Paris while running the system in Japan.

Let's see some running examples.

How to get the current system time in Java

The now() method of the LocalTime class by default returns the current time of the current running system(computer).

This method does not take any argument but returns an object of the LocalTime class. See the below Java code.

/* 
 *  Code example to get the current time in Java 8 and higher versions
 */
import java.time.LocalTime;
public class JExercise {
	public static void main(String[] args) {		

		// Current time
		LocalTime time = LocalTime.now();

		// Displaying time
		System.out.println("Time : "+time);

	}
}

Output:

Time : 21:00:20.154520013


How to get the current UTC time in Java

To get the current UTC (Coordinated Universal Time) Time in Java, we can use the overloaded method of the now(). This method takes an object of Clock as an argument and returns the UTC time.

Here, we used the Clock.systemUTC() method to get the default UTC time of the system, and display the result. See the below Java code.

import java.time.Clock;
/* 
 *  Code example to get the current time in Java 8 and higher versions
 */
import java.time.LocalTime;
public class JExercise {
	public static void main(String[] args) {		

		// Current time
		LocalTime time = LocalTime.now(Clock.systemUTC());

		// Displaying time
		System.out.println("Time : "+time);

	}
}

Output:

Time : 15:31:54.965383248
 

How to Get the current time based on the system default zone

The whole world is divided into several time zones and if you want to get the current time of the default zone in which your system is running then use the below code.

import java.time.Clock;
/* 
 *  Code example to get the current time in Java 8 and higher versions
 */
import java.time.LocalTime;
public class JExercise {
	public static void main(String[] args) {		

		// Current time
		LocalTime time = LocalTime.now(Clock.systemDefaultZone());

		// Displaying time
		System.out.println("Time : "+time);

	}
}

Output:

Time : 21:03:02.649812195
 

How to get the current time of the specified zone id in Java

We can use the ZoneId.of() method with the now() to get the current time of the specified zone. 

Here, we used this method to get the current time of India and France by using their zone id. I compiled a list of all the valid zone IDs in this article.

You can refer to this to get the current time of your desired zone.

import java.time.Clock;
/* 
 *  Code example to get the current time in Java 8 and higher versions
 */
import java.time.LocalTime;
import java.time.ZoneId;
public class JExercise {
	public static void main(String[] args) {		

		// Current time
		LocalTime time = LocalTime.now(ZoneId.of("Asia/Kolkata"));
		// Displaying time
		System.out.println("Time : "+time);

		time = LocalTime.now(ZoneId.of("Europe/Paris"));
		// Displaying time
		System.out.println("Time : "+time);

	}
}

Output:

Time : 21:05:09.020638340
Time : 16:35:09.023675493
 

Single Line Solution - Final Shot

Use this code to get the result in a single line of code. If you are a beginner, skip this code.

LocalTime.now();

This code will return the current time based on the system. You just copy it and get the result instantly.