Period minusMonths() method in Java with Examples Last Updated : 27 Nov, 2018 Comments Improve Suggest changes Like Article Like Report The minusMonths() method of Period class in Java is used to subtract the specified months from given period. This method operates only on MONTHS and does not affect other two YEAR, DAYS. Syntax: public Period minusMonths(long monthsToSubtract) Parameters: This method accepts a single parameter monthsToSubtract which is the number of months to be subtracted from the period. Return Value: This method returns a Period based on provided period in the input subtracting the specified number of months. It must not be null. Exceptions: It throws an ArithmeticException. This exception is caught if numeric overflow occurs. Below programs illustrate the above method: Program 1: Java // Java code to show the function minusMonths() // to subtract the number of months from given periods import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodClass { // Function to subtract two given periods static void subtractMonths(Period p1, int monthstoSubtract) { System.out.println(p1.minusMonths(monthstoSubtract)); } // Driver Code public static void main(String[] args) { // Defining first period int year = 4; int months = 11; int days = 10; Period p1 = Period.of(year, months, days); int monthstoSubtract = 8; subtractMonths(p1, monthstoSubtract); } } Output: P4Y3M10D Program 2: Java // Java code to show the function minusMonths() // to subtract the number of months from given periods import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodClass { // Function to subtract two given periods static void subtractMonths(Period p1, int monthstoSubtract) { System.out.println(p1.minusMonths(monthstoSubtract)); } // Driver Code public static void main(String[] args) { // Defining first period int year = -4; int months = -11; int days = -10; Period p1 = Period.of(year, months, days); int monthstoSubtract = -8; subtractMonths(p1, monthstoSubtract); } } Output: P-4Y-3M-10D Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#minusMonths-long- Comment More infoAdvertise with us Next Article Period minusMonths() method in Java with Examples B barykrg Follow Improve Article Tags : Misc Java Java-Functions Java-time package Java-Period +1 More Practice Tags : JavaMisc Similar Reads Period minus() method in Java with Examples The minus() method of Period class in Java is used to subtract the given amount of period from the specified period. This functions operates separately on YEAR, MONTH and DAY. Note: Normalization is not performed. 12 months and 1 year are different. Syntax: public Period minus(TemporalAmount amountT 2 min read Period ofMonths() method in Java with Examples The ofMonths() method of Period Class is used to obtain a period from given number of Months as parameter. This parameter is accepted in the form of integer. This method returns a Period with the given number of months. Syntax: public static Period ofMonths(int numberOfMonths) Parameters: This metho 2 min read Period minusDays() method in Java with Examples The minusDays() method of Period class in Java is used to subtract the days from this Period. This functions operates only on DAYS and does not affect YEAR and MONTH. Syntax: public Period minusDays(long daysToSubtract) Parameters: This method accepts a single parameter daysToSubtract which is the n 2 min read Period minusYears() method in Java with Examples The minusYears() method of Period class in Java is used to subtract the specified years from given period. This functions operates only on YEARS and does not affect other two MONTHS and DAYS. Syntax: public Period minusYears(long yearsToSubtract) Parameters: This method accepts a single parameter ye 2 min read Period of() method in Java with Examples The of() method of Period Class is used to obtain a period from given number of Years, Months, and Days as parameters. These parameters are accepted in the form of integers. This method returns a Period with the given number of years, months, and days. Syntax: public static Period of( int numberOfYe 2 min read 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 2 min read YearMonth minusMonths() method in Java with Examples The minusMonths() method of YearMonth class in Java is used to return a copy of this YearMonth with the specified number of months subtracted. Syntax: public YearMonth minusMonths( long monthsToSubtract) Parameter: This method accepts monthsToSubtract as parameters which represents months to subtrac 2 min read 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 a 2 min read Period parse() method in Java with Examples The parse() method of Period Class is used to obtain a period from given string in the form of PnYnMnD where nY means n years, nM means n months and nD means n days. Syntax: public static Period parse(CharSequence text) Parameters: This method accepts a single parameter text which is the String to b 2 min read 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 acc 2 min read Like