Period ofDays() method in Java with Examples
The ofDays() method of Period Class is used to obtain a period from given number of Days as parameter. This parameter is accepted in the form of integer. This method returns a Period with the given number of days.
Syntax:
public static Period ofDays(int numberOfDays)
Parameters: This method accepts a single parameter numberOfDays which is the number of Days to be parsed into an Period object.
Returns: This function returns the period which is the Period object parsed with the given number of Days.
Below is the implementation of Period.ofDays() method:
Example 1:
// Java code to demonstrate ofDays() method import java.time.Period; class GFG { public static void main(String[] args) { // Get the number of Days int numberOfDays = 5; // Parse the numberOfDays into Period // using ofDays() method Period p = Period.ofDays(numberOfDays); System.out.println(p.getYears() + " Years\n" + p.getMonths() + " Months\n" + p.getDays() + " Days"); } } |
0 Years 0 Months 5 Days
Example 2:
// Java code to demonstrate ofDays() method import java.time.Period; class GFG { public static void main(String[] args) { // Get the number of Days int numberOfDays = -5; // Parse the numberOfDays into Period // using ofDays() method Period p = Period.ofDays(numberOfDays); System.out.println(p.getYears() + " Years\n" + p.getMonths() + " Months\n" + p.getDays() + " Days"); } } |
0 Years 0 Months -5 Days
Reference: https://docs.oracle.com/javase/9/docs/api/java/time/Period.html#ofDays-int-
Recommended Posts:
- Duration ofDays(long) method in Java with Examples
- Period between() method in Java with Examples
- Period plus() method in Java with Examples
- Period of() method in Java with Examples
- Period get() method in Java with Examples
- Period from() method in Java with Examples
- Period negated() method in Java with Examples
- Period equals() method in Java with Examples
- Period isNegative() method in Java with Examples
- Period plusYears() method in Java with Examples
- Period normalized() method in Java with Examples
- Period getDays() method in Java with Examples
- Period ofMonths() method in Java with Examples
- Period plusMonths() method in Java with Examples
- Period withMonths() method in Java with Examples
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.



