Period ofYears() method in Java with Examples

The ofYears(int numberOfYears) method of Period class is used to get a period from the given number of years as parameter. The obtained period will represent the number of years. The unit month and days shall remain 0.

Syntax:

public static Period ofYears(int numberOfYears)

Parameters: This method accepts a single parameter numberOfYears of Integer type which represent the number of Years. It can be both negative and positive.

Return value: This method must return a Period, representing the number of Years.

Exception: This method does not throw any exception.



Below programs illustrate the ofYears() method of Period in Java:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Period ofYears(int numberOfYears) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create Period object
        Period period
            = Period.ofYears(5);
  
        // Print period
        System.out.println("Years: "
                           + period.getYears());
        System.out.println("Months: "
                           + period.getMonths());
        System.out.println("Days: "
                           + period.getDays());
    }
}

chevron_right


Output:

Years: 5
Months: 0
Days: 0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Period ofYears(int numberOfYears) method
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        int numberOfYears = -10;
  
        // Create Period object
        Period period
            = Period.ofYears(numberOfYears);
  
        // Print period
        System.out.println("Years: "
                           + period.getYears());
        System.out.println("Months: "
                           + period.getMonths());
        System.out.println("Days: "
                           + period.getDays());
    }
}

chevron_right


Output:

Years: -10
Months: 0
Days: 0

References:
https://docs.oracle.com/javase/10/docs/api/java/time/Period.html#ofYears(int)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.