English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Online Tools

Each loop

Java Arrays

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Exception Handling

resources

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection/O)

Java Reader/Writer

Other Java Topics

Java program checks the birthday and prints the 'Happy Birthday' message

Java Examples Comprehensive

In this example, we will learn how to check the current date using the birthday and print the 'Happy Birthday' message using Java.

Example: Check the birthday and return the message 'Happy Birthday'

import java.time.LocalDate;
import java.time.Month;
public class Main {
   public static void main(String args[]) {
    //Declare birthday variable
    int birthDate = 23;
    Month birthMonth = Month.SEPTEMBER;
    //Get the current date
    LocalDate currentDate = LocalDate.now();
    System.out.println("Today's date: ") + currentDate);
    //Get the 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.");
    }
   }
}

Output1

Today's date: 2020-08-28
Happy Birthday to You!!

In the above example,

  • LocalDate.now() - to return the current date

  • getDayOfMonth() - to return the current date

  • getMonth() - to return the current month

Here, we useif ... elseThe following statement is used to check if the current date matches the birthday. If it returns true, then printHappy BirthdayMessage.

Java Examples Comprehensive