Java Date Time - YearMonth format(DateTimeFormatter formatter) example








YearMonth format(DateTimeFormatter formatter) formats this year-month using the specified formatter.

Syntax

format has the following syntax.

public String format(DateTimeFormatter formatter)

Example

The following example shows how to use format.

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
/*from  www .  j  av  a  2s.  c  om*/
public class Main {
  public static void main(String[] args) {
    YearMonth y = YearMonth.now();
    String s = y.format(DateTimeFormatter.ofPattern("yyyy MM"));
    System.out.println(s);

  }
}

The code above generates the following result.