Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is Period.getMonths() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

getMonths() is an instance method of the Period class which is used to get the number of years of the Period instance.

The getMonths() method is defined in the Period class. The Period class is defined in the java.time package. To import the Period class check the following import statement.

import java.time.Period;

Syntax


public int getMonths()

Parameters

The method has no parameters.

Return value

This method returns the number of months of the Period object.

Code

In the code below, we retrieve the number of the months of the Period objects using the getMonths() method and print the returned value to the console.

import java.time.Period;
public class Main{
public static void main(String[] args) {
int numYears = 5;
int numMonths = 10;
int numDays = 13;
Period period = Period.of(numYears, numMonths, numDays);
System.out.println("The number of months of the period " + period + " is " + period.getMonths());
numYears = -5;
numMonths = -10;
numDays = -13;
period = Period.of(numYears, numMonths, numDays);
System.out.println("The number of months of the period " + period + " is " + period.getMonths());
}
}

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring