Javaexercise.com

How To Get Current Localdatetime In Java?

To get the current date-time by using the LocalDateTime class object, we can use its now() method.

This is a static method and returns an object of the LocalDateTime class.

public static LocalDateTime now()

Package Name: java.time;

Class Name: LocalDateTime

Return Value: It returns the current date-time from the System clock in the default time zone.

Parameters: No parameter required.

Exceptions: No exception.

Version: Since 1.8

This method has its overloading methods that are we will discuss later in the article, step by step.

Get the current local date-time with the default time zone in Java

Here, we used the now() method that returns the current date and time from the system. It returns the current date-time from the system.

/* 
 *  Code example to get current local datetime in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// Current date and time
		LocalDateTime date = LocalDateTime.now();

		// Displaying date and time
		System.out.println("Date : "+date);
		
	}
}

Output:

Date : 2022-03-17T10:14:37.015741254

Get the current local date-time with Clock in Java

If you want to get the current local date-time from the specific clock then use the below method.

public static LocalDateTime now(Clock clock)

Package Name: java.time;

Class Name: LocalDateTime

Return Value: It returns the current local date-time from the specified clock.

Parameters: It takes a single Clock type parameter.

Exceptions: No exception

Version: Since 1.8

Let's see the code example below:

/* 
 *  Code example to get current local datetime in Java
 */
import java.time.LocalDateTime;
import java.time.Clock;
public class JExercise {
	public static void main(String[] args) {		

		// Current date and time from the specified clock.
		LocalDateTime date = LocalDateTime.now(Clock.systemUTC());

		// Displaying date and time
		System.out.println("Date : "+date);

	}
}

Output:

Date : 2022-03-17T05:05:19.040114214

Get the current local date-time with a time-zone in Java

If you want to get the current local date-time of a specific time-zone the use the below method.

public static LocalDateTime now(ZoneId zone)

Package Name: java.time;

Class Name: LocalDateTime

Return Value: It returns the current date-time from the system clock in the specified time zone.

Parameters: It takes a single ZoneId type parameter.

Exceptions: No exception

Version: Since 1.8

Let's see a code example of this method:

Here, we specified the time zone to get the specific date-time.

You can refer to this time-zone table for more details about the supported time zones in the Java date and time API.

/* 
 *  Code example to get current local datetime in Java
 */
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.Clock;
import java.time.Instant;
public class JExercise {
	public static void main(String[] args) {		

		// Current date and time from the system clock in the specified time-zone.
		LocalDateTime date = LocalDateTime.now(Clock.system(ZoneId.of("Europe/Paris"))); // Europe

		// Displaying date and time
		System.out.println("Date : "+date);

		// Current date and time from the system clock in the specified time-zone.
		date = LocalDateTime.now(Clock.system(ZoneId.of("Asia/Kolkata"))); // Asia

		// Displaying date and time
		System.out.println("Date : "+date);

	}
}

Output:

Date : 2022-03-17T06:03:42.774263728
Date : 2022-03-17T10:33:42.778146170