Java program to check the birthday and print happy birthday message



In this article, we will understand how to check the birthday and print Happy Birthday message. Checking the birthday is achieved by comparing the present day and the given birthday.

Problem Statement

Write a program that checks if today's date matches a predefined birthday date. If the dates match, the program should print a "Happy Birthday" message otherwise, it should indicate that today is not the birthday. Below is a demonstration of the same ?

Input

Birthday Date: 15 July

Output

Today's Date is 20-12-2021
Today is not my birthday

Different ways to check the birthday

Using the LocalDate Class

The LocalDate class is imported from java.time package. The LocalDate class represents a date without a time zone in the ISO-8601 calendar system.

Creating an instance to store the current date using LocalDate:

LocalDate current_date = LocalDate.now();

Steps to check the birthday usingLocalDate Class

  • We will retrieve the current date by using LocalDate.now().
  • Extract the day using getDayOfMonth() and the month using getMonth() from the current date.
  • Using an if loop, we can compare the current month and date value with the input date and month values, respectively. If the values match, the result is true.
  • If the current day and month match the birthday day and month, print "HAPPY BIRTHDAY TO YOU !!" otherwise it will print "Your birthday is not today".

Example

Below is an example to check the birthday and print a happy birthday message using LocalDate class:

import java.time.LocalDate;
import java.time.Month;
public class HappyBirthday {
   public static void main(String args[]) {
      int date_of_birth = 15;
      Month month_of_birth = Month.JULY;
      System.out.println("The required packages have been imported");
      LocalDate current_date = LocalDate.now();
      System.out.println("Today's Date is " + current_date);
      System.out.println("The birthday is defined as : " +date_of_birth + " " +month_of_birth);
      int date = current_date.getDayOfMonth();
      Month month = current_date.getMonth();
      if(date == date_of_birth && month == month_of_birth) {
         System.out.println("HAPPY BIRTHDAY TO YOU !!");
      } else {
         System.out.println("Your birthday is not today ");
      }
   }
}

Output

The required packages have been imported
Today's Date is 2022-02-09
The birthday is defined as : 15 JULY
Your birthday is not today

Using SimpleDateFormat Class

The SimpleDateFormat from java.text package. It formats dates as per the given pattern. It is also used to parse dates from string where string contains date in mentioned format.

Creating an instance using SimpleDateFormat: 

SimpleDateFormat s = new SimpleDateFormat("MM-dd");
  • We will create a SimpleDateFormat object with the format "MM-dd" and use new Date() to get the current date.
  • Format the current date to a string using the SimpleDateFormat object.
  • Set the birthday string to "10-15".
  • If the formatted current date string matches the birthday string, print "Happy Birthday!!". - Otherwise, print "Today is not your birthday".

Example: Checking Birthday Using SimpleDateFormat

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
   public static void main(String[] args) {
      SimpleDateFormat s = new SimpleDateFormat("MM-dd");
      Date today = new Date();
      String todayString = s.format(today);
      String myBirthdayString = "10-15"; // Assuming your birthday is October 15th
      System.out.println("The birthday date is defined as " +myBirthdayString);
      if (todayString.equals(myBirthdayString)) {
         System.out.println("Happy Birthday!!");
      } else {
         System.out.println("Today is not your birthday");
      }
   }
}

Output

The birthday date is defined as October 15th
Happy Birthday!!
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-06-06T14:50:13+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements