Year parse(CharSequence) method in Java with Examples
Last Updated :
30 Jul, 2019
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:
import java.time.*;
public class GFG {
public static void main(String[] args)
{
Year year = Year.parse( "2019" );
System.out.println( "Year Parsed:"
+ year);
}
}
|
Program 2:
import java.time.*;
public class GFG {
public static void main(String[] args)
{
Year year = Year.parse( "2018" );
System.out.println( "Year Parsed:"
+ year);
}
}
|
References:
https://docs.oracle.com/javase/10/docs/api/java/time/Year.html#parse(java.lang.CharSequence)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...