English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this example, we will learn how to check the current date using the birthday and print the 'Happy Birthday' message using Java.
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.