Year now() method in Java with examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

2018

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#now–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.