Java Date Time - YearMonth toString() example








YearMonth toString() outputs this year-month as a String, such as 2014-12. The output will be in the format uuuu-MM:

Syntax

toString has the following syntax.

public String toString()

Example

The following example shows how to use toString.

import java.time.YearMonth;

public class Main {
  public static void main(String[] args) {
    YearMonth y = YearMonth.now();
    System.out.println(y.toString());

  }
}

The code above generates the following result.