Period minusYears() method in Java with Examples Last Updated : 28 Nov, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 yearsToSubtract which is the number of year to be subtracted from the period. Returns Value: This method returns a Period based on provided period in the input subtracting the specified number of years. 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 minusYears() // to subtract the number of years from given period import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodClass { // Function to subtract two given periods static void subtractYears(Period p1, int yearstoSubtract) { System.out.println(p1.minusYears(yearstoSubtract)); } // 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 yearstoSubtract = 8; subtractYears(p1, yearstoSubtract); } } Output: P-4Y11M10D Program 2: Java // Java code to show the function minusYears() // to subtract the number of years from given period import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodClass { // Function to subtract two given periods static void subtractYears(Period p1, int yearstoSubtract) { System.out.println(p1.minusYears(yearstoSubtract)); } // 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 yearstoSubtract = -8; subtractYears(p1, yearstoSubtract); } } Output: P12Y11M10D Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#minusYears-long- Comment More infoAdvertise with us Next Article Period minus() 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 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 minusMonths() method in Java with Examples 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 month 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 Year minusYears() method in Java with Examples minusYears() method of the Year class used to a copy of this Year after subtracting the specified number of years from this year. This instance is immutable and unaffected by this method call. Syntax: public Year minusYears(long yearsToSubtract) Parameters: This method accepts yearsToSubtract as par 2 min read Like