Period withYears() method in Java with Examples Last Updated : 28 Nov, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The withYears() method of Period Class is used to obtain a period with specified number of years. This number of years is passed as the parameter as integer value. Syntax: public Period withYears(int numberOfYears) Parameters: This method accepts a parameter numberOfYears which is the number of years to be changed in this Period. Returns: This function returns a Period with the number of years passed as the parameter. Below is the implementation of Period.withYears() method: Example 1: Java // Java code to demonstrate withYears() method import java.time.Period; class GFG { public static void main(String[] args) { // Get the String to be withYearsd String period = "P1Y2M21D"; // Parse the String into Period Period p = Period.parse(period); System.out.println("Period: " + p); // Get the number of years int numberOfYears = 5; // Change the numberOfYears of this Period // using withYears() method System.out.println("New Period: " + p.withYears(numberOfYears)); } } Output: Period: P1Y2M21D New Period: P5Y2M21D Example 2: Java // Java code to demonstrate withYears() method import java.time.Period; class GFG { public static void main(String[] args) { // Get the String to be withYearsd String period = "-P1Y2M21D"; // Parse the String into Period Period p = Period.parse(period); System.out.println("Period: " + p); // Get the number of years int numberOfYears = -5; // Change the numberOfYears of this Period // using withYears() method System.out.println("New Period: " + p.withYears(numberOfYears)); } } Output: Period: P-1Y-2M-21D New Period: P-5Y-2M-21D Reference: https://docs.oracle.com/javase/9/docs/api/java/time/Period.html#withYears-int- Comment More infoAdvertise with us Next Article Year query() 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 withDays() method in Java with Examples The withDays() method of Period Class is used to obtain a period with specified number of days. This number of days is passed as the parameter as integer value. Syntax: public Period withDays(int numberOfDays) Parameters: This method accepts a parameter numberOfDays which is the number of days to be 2 min read Period withMonths() method in Java with Examples The withMonths() method of Period Class is used to obtain a period with specified number of months. This number of months is passed as the parameter as integer value. Syntax: public Period withMonths(int numberOfMonths) Parameters: This method accepts a parameter numberOfMonths which is the number o 2 min read Year of() method in Java with Examples The of() method of Year class in Java is used to return an Year instance. It accepts an year according to the proleptic ISO calendar system and returns an instance of Year with the specified isoYear. Syntax: public static Year of(int isoYear) Parameter: It accepts a single integral value isoYear as 1 min read Year query() Method in Java with Examples query() method of an Year class used to query this Year using the specified query as parameter.The TemporalQuery object passed as parameter define the logic to be used to obtain the result from this Year. Syntax: public <R> R query(TemporalQuery<R> query) Parameters: This method accepts 2 min read Period plus() method in Java with Examples The plus() method of Period class in Java is used to add the given amount of period to 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 plus(TemporalAmount amountToAdd) Par 2 min read Year now() method in Java with examples The now() method of Year class in Java is used to return the current year from the system clock in the default time-zone. Syntax: public static Year now() or, public static Year now(Clock clock) or, public static Year now(ZoneId zone) Parameter: The parameter is optional for this method as shown in 2 min read Like