Java birthday program – Java Program to Print Happy Birthday on Your Birthday

Java birthday program: In this article we will see how to print ‘Happy Birthday’ on your birthday by using Java programming language.

Java Program to Print Happy Birthday on Your Birthday

Birthday is the anniversary of the day on which the person was born. Generally people celebrate this day to with family, friends, cakes, gifts and many more surprises to make it memorable. That day starts with a warm wish like ‘Happy Birthday’ and many more.

Here we will write a Java program to check our birthday and print ‘Happy Birthday’.

Method-1: Java Program to Print Happy Birthday on Your Birthday By Using == Operator

Approach:

  • Declare birth date and month.
  • By using LocalDate class get the current date.
  • Call getDayOfMonth() and getMonth() method by using object of LocalDate class to get the current month and day.
  • Compare the user declared date and month with current date and month by using == operator.
  • If both date and month matches then it’s birthday else it’s not birthday.

Program:

import java.util.Scanner;
import java.time.LocalDate;
import java.time.Month;

public class Main
{
    public static void main(String args[]) 
    {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Asking user to enter his/her name
        System.out.println("Enter your name: ");
        String name=sc.nextLine();
        //birth date 
        int birthDate = 21;
        //birth month
        Month birthMonth = Month.JULY;
        //current date
        LocalDate current_date = LocalDate.now();
        
        System.out.println("Today's Date: " + current_date);
        System.out.println("The birth Date: " +birthDate + " " +birthMonth);
        
        //getting current month
        int date = current_date.getDayOfMonth();
        //getting current month
        Month month = current_date.getMonth();
        
        if(date == birthDate && month == birthMonth) 
        { 
            System.out.println("Happy Birthday To You "+ name);
        } 
        else 
        { 
            System.out.println(name+" today is not your birthday");
        } 
    }
}

Output:

Enter your name: 
Satya
Today's Date: 2022-07-21
The birth Date: 21 JULY
Happy Birthday To You Satya

Method-2: Java Program to Print Happy Birthday on Your Birthday By Using compareTo() Method

Approach:

  • Create object of SimpleDateFormat class and pass parameter as MM-dd format.
  • Declare birth date in MM-dd format.
  • Get today’s date in MM-dd format.
  • Take a if condition and by using compareTo() method compare today’s date and birth date.
  • If condition satisfies then it’s birthday else it’s not birthday.

Program:

import java.util.Scanner;
import java.time.LocalDate;
import java.time.Month;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main
{
   public static void main(String[] args) throws ParseException 
   {
      //Scanner class object created 
      Scanner sc=new Scanner(System.in); 
      //Asking user to enter his/her name 
      System.out.println("Enter your name: "); 
      String name=sc.nextLine();
      
      //created object of SimpleDateFormat class
      //date format in MM-dd
      SimpleDateFormat s = new SimpleDateFormat("MM-dd");
      
      //today's date
      Date today = s.parse("07-21");
      //birth date
      Date birthDate = s.parse("07-21");
      
      System.out.println("Your birth date is 21st July");
      
      //comparing today's date and birth date
      if (today.compareTo(birthDate) == 0) 
      {
    	  System.out.println("Happy Birthday To You "+ name);
      }      
      else 
      {
    	  System.out.println(name+" today is not your birthday");
      }
   }
}

Output:

Enter your name: 
Satya
Your birth date is 21st July
Happy Birthday To You Satya

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.