This post shows you how to get system current date time in java using its java 8 LocalTime API. Also, LocalTime API comes with few overloaded methods to gets date and time of different timezone.
LocalTime.now contains three overloaded method
public static LocalTime now()
It obtains the current time from the system clock in the default time-zone.
public static LocalTime now(ZoneId zone)
It obtains the current time from the system clock in the specified time-zone.
public static LocalTime now(Clock clock)
Obtains the current time from the specified clock.
Program:
import java.time.Clock; import java.time.LocalTime; import java.time.ZoneId; public class LocalTimeExample { public static void main(String[] args) { // get current time LocalTime currTime = LocalTime.now(); System.out.println("current time: "+currTime); LocalTime currTimeWithSystemZone = LocalTime.now(Clock.systemDefaultZone()); System.out.println("current time with default system time zone : "+currTimeWithSystemZone); LocalTime currTimeWithZone = LocalTime.now(ZoneId.of("Europe/Paris")); System.out.println("current time with Europe/Paris time zone : "+currTimeWithZone); } }
Output:
current time: 11:20:35.378
current time with default system time zone : 11:20:35.380
current time with Europe/Paris time zone : 06:50:35.383