Thursday, December 11, 2025
HomeLanguagesJavaYear parse(CharSequence) method in Java with Examples

Year parse(CharSequence) method in Java with Examples

The parse(CharSequence) method of Year class is used to get an instance of Year from a string such as ‘2018’ passed as parameter. 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)

Parameters: This method accepts only one parameter text which represents the text to parse such as “2021”.

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 text) method:

Program 1:




// Java program to demonstrate
// Year.parse(CharSequence text) method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a Year object
        // using parse(CharSequence text)
        Year year = Year.parse("2019");
  
        // print instance
        System.out.println("Year Parsed:"
                           + year);
    }
}


Output:

Year Parsed:2019

Program 2:




// Java program to demonstrate
// Year.parse(CharSequence text) method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a Year object
        // using parse(CharSequence text)
        Year year = Year.parse("2018");
  
        // print instance
        System.out.println("Year Parsed:"
                           + year);
    }
}


Output:

Year Parsed:2018

References:
https://docs.oracle.com/javase/10/docs/api/java/time/Year.html#parse(java.lang.CharSequence)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32444 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6813 POSTS0 COMMENTS
Nicole Veronica
11950 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12026 POSTS0 COMMENTS
Shaida Kate Naidoo
6944 POSTS0 COMMENTS
Ted Musemwa
7197 POSTS0 COMMENTS
Thapelo Manthata
6889 POSTS0 COMMENTS
Umr Jansen
6881 POSTS0 COMMENTS