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 accomplished 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

Check the Birthday Using LocalDate Class

Below are the steps to check the birthday using LocalDate class:

  • Import java.time.LocalDate and java.time.Month from java.time package.
  • Set variables for the birthday day (15) and month (Month.JULY).
  • Retrieve the current date using LocalDate.now().
  • Extract the day using getDayOfMonth() and the month using getMonth() from the current date.
  • Using an if loop, 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, print "Your birthday is not today".

Example: Checking Birthday Using 'LocalDate'

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

Code Explanation

The provided code uses Java's LocalDate and Month classes to check if today matches a specified birthday date. It initializes the birthday as July 15 and then retrieves the current date using LocalDate.now(). The code extracts the current day and month from the current date and compares them with the predefined birthday day and month. If the current date matches the birthday, it prints "HAPPY BIRTHDAY TO YOU !!", otherwise it prints "Your birthday is not today". This method ensures that the birthday check is based solely on the day and month, ignoring the year.

Check the Birthday Using 'SimpleDateFormat' Class

Below are the steps to check the birthday using SimpleDateFormat class:

  • Import SimpleDateFormat and Date from java.text and java.util packages.
  • Create a SimpleDateFormat object with the format "MM-dd".
  • 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!!

Code Explanation

The provided code uses SimpleDateFormat to check if today matches a specified birthday date. It initializes a SimpleDateFormat object with the "MM-dd" pattern to format dates as month and day. The code then gets the current date using new Date(), formats it to a string, and compares this string to a predefined birthday string ("10-15"). If the formatted current date matches the birthday string, it prints "Happy Birthday!!" otherwise, it prints "Today is not your birthday". This method ensures that the birthday check is based solely on the day and month.

Updated on: 19-Jul-2024

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements