Java YearMonth class

Introduction

Java YearMonth class represents a year and a month, for example, 2020-05, 2013-09, etc.

import java.time.Month;
import java.time.YearMonth;

public class Main {
  public static void main(String[] args) {
    // Use YearMonth
    YearMonth ym1 = YearMonth.of(2020, Month.MAY); // 2020-05

    // Get the number of days in the month
    int monthLen = ym1.lengthOfMonth(); 
    System.out.println("Days in month in " + ym1 + ": " + monthLen);

    // Get the number of days in the year
    int yearLen = ym1.lengthOfYear();
    System.out.println("Days in year in " + ym1 + ": " + yearLen);
  }/*from   w  w  w .ja  v  a 2  s . c om*/
}



PreviousNext

Related