Java Date Time - YearMonth parse(CharSequence text, DateTimeFormatter formatter) example








YearMonth parse(CharSequence text, DateTimeFormatter formatter) creates an instance of YearMonth from a text string using a specific formatter.

Syntax

parse has the following syntax.

public static YearMonth parse(CharSequence text,   DateTimeFormatter formatter)

Example

The following example shows how to use parse.

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
//from  ww  w.java  2 s.  co  m
public class Main {
  public static void main(String[] args) {
    YearMonth y = YearMonth.parse("2013 09",DateTimeFormatter.ofPattern("yyyy MM"));
    
    System.out.println(y);

  }
}

The code above generates the following result.