Join our newsletter for the latest updates.
Java Program to Check the birthday and print Happy Birthday message

Java Program to Check the birthday and print Happy Birthday message

In this example, we will learn to check the current day with the birthday and print the Happy Birthday message in Java.

To understand this example, you should have the knowledge of the following Java programming topics:


Example: Check birthday and return Happy Birthday message

import java.time.LocalDate;
import java.time.Month;

public class Main {
   public static void main(String args[]) {

    // declare variables for birthday
    int birthDate = 23;
    Month birthMonth = Month.SEPTEMBER;

    // get current date
    LocalDate currentDate = LocalDate.now();
    System.out.println("Todays Date: " + currentDate);

    // get current date and month
    int date = currentDate.getDayOfMonth();
    Month month = currentDate.getMonth();

    if(date == birthDate && month == birthMonth) {
      System.out.println("HAPPY BIRTHDAY TO YOU !!");
    }
    else {
      System.out.println("Today is not my birthday.");
    }
   }
}

Output 1

Todays Date: 2020-07-28
HAPPY BIRTHDAY TO YOU !!

In the above example,

  • LocalDate.now() - returns the current date
  • getDayOfMonth() - returns the current day
  • getMonth() - returns the current month

Here, we have used the if...else statement to check if the current date matches the birthdate. If true, the Happy Birthday message is printed.