Wednesday, July 3, 2024
HomeLanguagesJavaPeriod ofDays() method in Java with Examples

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


Output:

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


Output:

0 Years
0 Months
-5 Days

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments