Java LocalDate now() Method Example [2021]
Java LocalDate now() is an overloaded method in LocalDate class with three variants
- public static LocalDate now()
- public static LocalDate now(ZoneId zone)
- public static LocalDate now(Clock clock)
We have also covered LocalDateTIme.now() in earlier tutorials.
In this tutorial, we will see each method in action one by one
Java LocalDate now
1. public static LocalDate now()
- This method gets the current date from the system clock in the default time-zone.
- This method restricts us to use an alternate clock for testing as the clock is hard-coded internally.
Returns:
the current date using the system clock with default time-zone which is not null.
Example:
import java.time.LocalDate;
public class LocalDateNowEx1 {
public static void main(String[] args) {
//Java LocalDate now() Example
LocalDate now = LocalDate.now();
System.out.println("LocalDate now() : " + now);
}
}
Output:
LocalDate now() : 2020-07-08
2. public static LocalDate now(ZoneId zone)
- This method gets the current date from the system clock in the specified time-zone.
- This method will query the system clock to get the current date.
- Specifying the time-zone avoids dependence on the default time-zone.
- This method restricts us to use an alternate clock for testing as the clock is hard-coded internally.
Parameters:
zone - the zone ID to use which is not null.
Returns:
the current date using the system default clock which is not null.
Example:
import java.time.LocalDate;
import java.time.ZoneId;
public class LocalDateNowEx2 {
public static void main(String[] args) {
// Java LocalDate now(ZoneId zone) Example
LocalDate now = LocalDate.now(ZoneId.systemDefault());
System.out.println("LocalDate now(SystemDefault zone) : " + now);
LocalDate now1 = LocalDate.now(ZoneId.of("America/Phoenix"));
System.out.println("LocalDate now(America/Phoenix) : " + now1);
}
}
Output:
LocalDate now(SystemDefault zone) : 2020-07-08
LocalDate now(America/Phoenix) : 2020-07-08
3. public static LocalDate now(Clock clock)
- This method gets the current date from the specified clock.
- This will query the specified clock to obtain the current date - today.
- This method allows us to use an alternate clock specified by the user.
Parameters:
clock - the clock to use which is not null.
Returns:
the current date which is not null.
Example:
import java.time.Clock;
import java.time.LocalDate;
import java.time.ZoneId;
public class LocalDateNowEx3 {
public static void main(String[] args) {
//Java LocalDate now(Clock clock) Example
LocalDate now = LocalDate.now(Clock.systemDefaultZone());
System.out.println("Using default Zone Clock : " + now);
LocalDate now1 = LocalDate.now(Clock.systemUTC());
System.out.println("Using system Utc Clock : " + now1);
LocalDate now2 = LocalDate.now(Clock.system(ZoneId.of("US/Eastern")));
System.out.println("Using Zone Specific Clock : " + now2);
}
}
Output:
Using default Zone Clock : 2020-07-08
Using system Utc Clock : 2020-07-08
Using Zone Specific Clock : 2020-07-08
Conclusion:
In this tutorial, we have covered Java LocalDate now() methods with Examples.
More Examples you may like
References:
Comments
Post a Comment
If you have any doubts, Please let me know.