The parse(CharSequence) method of YearMonth class used to get an instance of YearMonth from a string such as ‘2018-12’ passed as parameter.The string must have a valid value that can be converted to a YearMonth object. The format of String must be uuuu-MM. YearMonths outside the range 0000 to 9999 must be prefixed by the plus or minus symbol.
public static YearMonth parse(CharSequence text)
This method accepts only one parameter text which represents the text to parse and format of this String must be like uuuu-MM.
This method returns the parsed YearMonth.
Exception: This method throws following Exceptions:
- DateTimeException – if the text cannot be parsed.
Below programs illustrate the parse(CharSequence text) method:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.