Skip to content
Related Articles

Related Articles

Year now() method in Java with examples
  • Last Updated : 27 Nov, 2018
GeeksforGeeks - Summer Carnival Banner

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




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




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

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :