The now() method of Year class in Java is used to return the current year from the system clock in the default time-zone.
Syntax:
public static Year now() or, public static Year now(Clock clock) or, public static Year now(ZoneId zone)
Parameter: The parameter is optional for this method as shown in the above syntax.
Return Value: It returns the current year from the system clock in the default time-zone if no parameter is specified otherwise it uses the specified clock and time zone to return the current year. It can never return a NULL value
Below program illustrate the length() method of Year in Java:
Program 1:
// Program to illustrate the now() method import java.util.*; import java.time.*; public class GfG { public static void main(String[] args) { // Create a Year object using now() method // It will return the current year from the // system clock in the default time-zone. Year thisYear = Year.now(); // Print the year object System.out.println(thisYear); } } |
2018
Program 2: If a clock is specified as a parameter to the now() method. In that case, it will use the default time zone but not the system clock, it will instead use the clock passed as parameter to it in the default time-zone.
// Program to illustrate the now() method import java.util.*; import java.time.*; public class GfG { public static void main(String[] args) { // Create a Year object using now() method // It will return the current year from the // clock specified in the default time-zone. Year thisYear = Year.now(Clock.systemUTC()); // Print the year object System.out.println(thisYear); } } |
2018
Program 3: If a zone is specified as a parameter to the now() method. In that case, it will not use the default time zone, it will instead use the system-clock in the time-zone provided as a parameter to it.
// Program to illustrate the now() method import java.util.*; import java.time.*; public class GfG { public static void main(String[] args) { // Create a Year object using now() method // It will return the current year from the // system-clock in the time-zone specified Year thisYear = Year.now(ZoneId.systemDefault()); // Print the year object System.out.println(thisYear); } } |
2018
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#now–