VOOZH about

URL: https://www.geeksforgeeks.org/java/year-parsecharsequencedatetimeformatter-method-in-java-with-examples/

⇱ Year parse(CharSequence,DateTimeFormatter) method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Year parse(CharSequence,DateTimeFormatter) method in Java with Examples

Last Updated : 28 Dec, 2022

The Year.parse(CharSequence, DateTimeFormatter) method of Year class is used to get an instance of Year from a string such as ‘2018’ passed as parameter using a specificDateTimeFormatter.The Year is parsed using a specific DateTimeFormatter. The string must have a valid value that can be converted to a Year. Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol. Syntax:

public static Year parse(CharSequence text,
 DateTimeFormatter formatter)

Parameters: This method accepts two parameters text which represents the text to parse such as "2021" and formatter which represents the formatter to use. Return value: This method returns the parsed year. Exception: This method throws following Exceptions:

  • DateTimeException - if the text cannot be parsed.

Below programs illustrate the parse(CharSequence, DateTimeFormatter) method: Program 1: 

Output:
Year Parsed:2018

Program 2: 

Comment
Article Tags: