Period parse() method in Java with Examples Last Updated : 28 Nov, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 be parsed. Returns: This function returns the period which is the parsed representation of the String given as the parameter Below is the implementation of Period.parse() method: Example 1: Java // Java code to demonstrate parse() method // to obtain period from given string import java.time.Period; class GFG { public static void main(String[] args) { // Get the String to be parsed String period = "P1Y2M21D"; // Parse the String into Period // using parse() method Period p = Period.parse(period); System.out.println(p.getYears() + " Years\n" + p.getMonths() + " Months\n" + p.getDays() + " Days"); } } Output: 1 Years 2 Months 21 Days Example 2: Java // Java code to demonstrate parse() method // to obtain period from given string import java.time.Period; class GFG { public static void main(String[] args) { // Get the String to be parsed String period = "-P1Y2M21D"; // Parse the String into Period // using parse() method Period p = Period.parse(period); System.out.println(p.getYears() + " Years\n" + p.getMonths() + " Months\n" + p.getDays() + " Days"); } } Output: -1 Years -2 Months -21 Days Reference: https://docs.oracle.com/javase/9/docs/api/java/time/Period.html#parse-java.lang.CharSequence- Comment More infoAdvertise with us Next Article Period ofDays() 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 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 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 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 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 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 negated() method in Java with Examples The negated() method of Period class in Java is used to return a new instance of Period after negating all the elements of the period YEAR, MONTH, DAY. Syntax: public Period negated() Parameters: This method does not accepts any parameter. Return Value: This method returns a new instance of Period a 2 min read Like