Monday, June 15, 2026
HomeLanguagesJavaYearMonth parse(CharSequence,DateTimeFormatter) method in Java with Examples

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

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. Syntax:

public static YearMonth parse(CharSequence text,
                         DateTimeFormatter formatter)

Parameters: This method accepts two parameters text which represents the text to parse and formatter which represents the formatter to use. Return value: 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: Program 1: 

Java




// Java program to demonstrate
// YearMonth.parse(CharSequence, DateTimeFormatter) method
 
import java.time.*;
import java.time.format.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create a formatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-MM");
 
        // create a YearMonth object
        // using parse(CharSequence, DateTimeFormatter)
        YearMonth yearMonth = YearMonth.parse("18-02", formatter);
 
        // print instance
        System.out.println("YearMonth Parsed:"
                           + yearMonth);
    }
}


Output:

YearMonth Parsed:2018-02

Program 2: 

Java




// Java program to demonstrate
// YearMonth.parse(CharSequence, DateTimeFormatter) method
 
import java.time.*;
import java.time.format.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create a formatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
 
        // create a YearMonth object
        // using parse(CharSequence, DateTimeFormatter)
        YearMonth yearMonth = YearMonth.parse("2100-09", formatter);
 
        // print instance
        System.out.println("YearMonth Parsed:"
                           + yearMonth);
    }
}


Output:

YearMonth Parsed:2100-09

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

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

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS