Year now() method in Java with examples
Last Updated :
27 Nov, 2018
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:
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
Year thisYear = Year.now();
System.out.println(thisYear);
}
}
|
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.
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
Year thisYear = Year.now(Clock.systemUTC());
System.out.println(thisYear);
}
}
|
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.
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
Year thisYear = Year.now(ZoneId.systemDefault());
System.out.println(thisYear);
}
}
|
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#now–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...