Java Data Type How to - Check for recurring events e.g. birthday in Java 8








Question

We would like to know how to check for recurring events e.g. birthday in Java 8.

Answer

import java.time.LocalDate;
import java.time.MonthDay;
/*from  w  w w  .  j a  v a2  s .  co  m*/
public class Main {
  public static void main(String[] argv) {
    LocalDate dateOfBirth = LocalDate.now();
    MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(),
        dateOfBirth.getDayOfMonth());
    MonthDay currentMonthDay = MonthDay.from(LocalDate.now());

    if (currentMonthDay.equals(birthday)) {
      System.out.println("Yes!!");
    } else {
      System.out.println("Sorry, today is not your birthday");
    }
  }
}

The code above generates the following result.