LocalDateTime now() Java Examples
In this tutorial, we will cover how we can use LocalDateTime now() method in Java with Examples.
In the earlier tutorial, we have covered LocalDateTime isBefore() method in Java which is used to compare two DateTime objects whether one is before than another one.
Here is the brief intro about what is LocalDateTime class in java.
LocalDateTime
- LocalDateTime is an instance of date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.
- LocalDateTime is an immutable date-time object that represents a date-time, often viewed as a year-month-day-hour-minute-second.
There are three now overloaded methods provided by the LocalDateTime class. Let's See each one of them with Example.
LocalDateTime now() Methods
1. public static LocalDateTime now()
This now() method with no parameter gets the current date-time from the system clock in the default time-zone.
Using this method we are restricted to use inbuild Clock used in this method internally.
Returns:
It returns the current date-time using the system clock and default time-zone, which will be not null.
Example:
import java.time.LocalDateTime;
public class LocalDateTimeNow1 {
public static void main(String[] args) {
// Create LocalDateTime instance using now()
LocalDateTime time = LocalDateTime.now();
System.out.println("LocalDateTime now() : " + time);
// Prints LocalDateTime now() : 2020-06-28T20:26:29.183
}
}
Output:
This will print Current Date Time with System Default timezone.
LocalDateTime now() : 2020-06-28T20:26:29.183
2. public static LocalDateTime now(Clock clock)
This now(Clock clock) method with one parameter clock gets the current date-time from the specified clock. This will query the specified clock to obtain the current date-time.
Using this method gives us the liberty to use other Clock for any other zone.
Parameters:
clock - the clock to use which is not null.
Returns:
the current date-time which is not null.
Example:
import java.time.Clock;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class LocalDateTimeNow2 {
public static void main(String[] args) {
// With SystemDefaultZoneClock
LocalDateTime nowWithClock1 = LocalDateTime.now(Clock.systemDefaultZone());
System.out.println("Clock with DefaultTimeZone : " + nowWithClock1);
// With systemUTC Clock
LocalDateTime nowWithClock2 = LocalDateTime.now(Clock.systemUTC());
System.out.println("Clock with UTC Timezone : " + nowWithClock2);
// With clock made form ZoneId passed as US/Eastern
LocalDateTime nowWithClock3 = LocalDateTime.now(Clock.system(ZoneId.of("US/Eastern")));
System.out.println("Clock with US/Eastern Timezone : " + nowWithClock3);
// With clock modified using Duration with 4 hours added to defaulttimezone
LocalDateTime nowWithClock4 = LocalDateTime.now(Clock.offset(Clock.systemDefaultZone(), Duration.ofHours(4)));
System.out.println("Clock with modified time with Duration : " + nowWithClock4);
}
}
Output:
Clock with DefaultTimeZone : 2020-06-28T20:57:16.541
Clock with UTC Timezone : 2020-06-28T15:27:16.542
Clock with US/Eastern Timezone : 2020-06-28T11:27:16.547
Clock with modified time with Duration : 2020-06-29T00:57:16.563
2. public static LocalDateTime now(ZoneId zone)
This now(ZoneId zone) method with one parameter as zoneId instance gets the current date-time from the system clock in the specified time-zone.
- This will query the system clock to obtain the current date-time.
- Specifying the time-zone avoids dependence on the default time-zone.
- Using this method will restrict us to use the inbuilt clock which is hard-coded.
Parameters:
zone - the zone ID to use which is not null.
Returns:
the current date-time using the system clock which is not null.
Example:
import java.time.LocalDateTime;
import java.time.ZoneId;
public class LocalDateTimeNow3 {
public static void main(String[] args) {
// With System Default Timezone
LocalDateTime zoneIdTime1 = LocalDateTime.now(ZoneId.systemDefault());
System.out.println("LocalDateTime with System Default Timezone : "+zoneIdTime1);
// With Timezone Specified Asia/Calcutta
LocalDateTime zoneIdTime2 = LocalDateTime.now(ZoneId.of("Asia/Calcutta"));
System.out.println("LocalDateTime with Asia/Calcutta Timezone : "+zoneIdTime2);
// With Timezone Specified Antarctica/Palmer
LocalDateTime zoneIdTime3 = LocalDateTime.now(ZoneId.of("Antarctica/Palmer"));
System.out.println("LocalDateTime with Antarctica/Palmer Timezone : "+zoneIdTime3);
}
}
Output:
LocalDateTime with System Default Timezone : 2020-06-28T20:59:35.005
LocalDateTime with Asia/Calcutta Timezone : 2020-06-28T20:59:35.006
LocalDateTime with Antarctica/Palmer Timezone : 2020-06-28T12:29:35.007
Conclusion:
In this tutorial, we have covered LocalDateTime now() Java Examples. We have covered all the three now() overloaded methods and these methods are handy to use to get current LocalDateTime of system default zone or any other timezone we specify,
Happy Learning!!!
More Examples you may like
More Examples you may like
Comments
Post a Comment
If you have any doubts, Please let me know.