Example: Check birthday and return Happy Birthday message
import java.time.LocalDate;
import java.time.Month;
public class Main {
public static void main(String args[]) {
// declare variables for birthday
int birthDate = 23;
Month birthMonth = Month.SEPTEMBER;
// get current date
LocalDate currentDate = LocalDate.now();
System.out.println("Todays Date: " + currentDate);
// get 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.");
}
}
}
Output 1
Todays Date: 2020-07-28 HAPPY BIRTHDAY TO YOU !!
In the above example,
- LocalDate.now() - returns the current date
- getDayOfMonth() - returns the current day
- getMonth() - returns the current month
Here, we have used the if...else statement to check if the current date matches the birthdate. If true
, the Happy Birthday message is printed.
Also Read: