Period ofWeeks() method in Java with Examples

The ofWeeks() method of Period Class is used to obtain a period from given number of Weeks as parameter. This parameter is accepted in the form of integer. This method returns a Period with the given number of weeks.

Syntax:

public static Period ofWeeks(int numberOfWeeks)

Parameters: This method accepts a single parameter numberOfWeeks which is the number of Weeks to be parsed into an Period object.



Returns: This function returns the period which is the Period object parsed with the given number of Weeks.

Below is the implementation of Period.ofWeeks() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate ofWeeks() method
  
import java.time.Period;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Get the number of Weeks
        int numberOfWeeks = 5;
  
        // Parse the numberOfWeeks into Period
        // using ofWeeks() method
        Period p = Period.ofWeeks(numberOfWeeks);
  
        System.out.println(p.getYears() + " Years\n"
                           + p.getMonths() + " Months\n"
                           + p.getDays() + " Days");
    }
}

chevron_right


Output:

0 Years
0 Months
35 Days

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate ofWeeks() method
  
import java.time.Period;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Get the number of Weeks
        int numberOfWeeks = -5;
  
        // Parse the numberOfWeeks into Period
        // using ofWeeks() method
        Period p = Period.ofWeeks(numberOfWeeks);
  
        System.out.println(p.getYears() + " Years\n"
                           + p.getMonths() + " Months\n"
                           + p.getDays() + " Days");
    }
}

chevron_right


Output:

0 Years
0 Months
-35 Days

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/Period.html#ofWeeks-int-



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.