Java Data Type How to - Create fixed date like credit card expiry in YearMonth in Java 8








Question

We would like to know how to create fixed date like credit card expiry in YearMonth in Java 8.

Answer

import java.time.Month;
import java.time.YearMonth;
// w ww . j  a  v  a2s .  c  om
public class Main {
  public static void main(String[] argv) {
    YearMonth currentYearMonth = YearMonth.now();
    System.out.printf("Days in month year %s: No of days: %s \n",
        currentYearMonth, currentYearMonth.lengthOfMonth());
    YearMonth creditCardExpiry = YearMonth.of(2018, Month.FEBRUARY);
    System.out.printf("Your credit card expires on %s: No of days: %s \n",
        creditCardExpiry, creditCardExpiry.lengthOfMonth());

  }
}

The code above generates the following result.