The YearMonth.parse(CharSequence, DateTimeFormatter) method of YearMonth class used to get an instance of YearMonth from a string such as ‘2018’ passed as a parameter using a specificDateTimeFormatter.The YearMonth is parsed using a specific DateTimeFormatter. The string must have a valid value that can be converted to a YearMonth.
public static YearMonth parse(CharSequence text, DateTimeFormatter formatter)
This method accepts two parameters text which represents the text to parse and formatter which represents the formatter to use.
This method returns the parsed YearMonth.
Exception: This method throws following Exceptions:
- DateTimeException – This exception is thrown if the text cannot be parsed.
Below programs illustrate the parse(CharSequence, DateTimeFormatter) 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.