
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
Below is a demonstration of the same −
Input
Suppose our input is −
Birthday Date: 15 July
Output
The desired output would be −
Today’s Date is 20-12-2021 Today is not my birthday
Algorithm
Step 1 - START Step 2 - Declare variables for month and dates values namely month_of_birth and date_of_birth Step 3 - Read the required values from the user/ define the values Step 4 - Use LocalDate.now() function to get the current date and store it in the variable. Step 5 – 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. Step 5- Display the result Step 6- Stop
Example 1
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
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Birthday { public static void main(String[] args) throws ParseException { SimpleDateFormat s = new SimpleDateFormat("MM-dd"); Date today = s.parse("10-15"); Date my_birthday_date = s.parse("10-15"); System.out.println("The birthday date is defined as October 15th" ); if (today.compareTo(my_birthday_date) == 0) { System.out.println("Happy Birthday!!"); } else { System.out.println("Today is not tour birthday"); } } }
Output
The birthday date is defined as October 15th Happy Birthday!!
- Related Articles
- Haskell Program to Check the birthday and print Happy Birthday message
- C++ program to print Happy Birthday
- Birthday Paradox in C++
- Birthday Paradox in Python
- Birthday attack in Cryptography
- Birthday Reminder Application in Python
- How to Calculate Age in Excel from Birthday?
- How to Generate Random Birthday Wishes using JavaScript?
- How to calculate days until next birthday in Excel?
- Python program to print all Happy numbers between 1 and 100
- Why do you celebrate your birthday every year?
- How to calculate the next age of a person’s birthday in Excel?
- Write a program to print message without using println() method in java?
- Python program to check if the given number is Happy Number
- Sheila's birthday is in May. The date is a multiple of 6 and 9. Can you guess her birthday?a. May 9thb. May 18thc. May 27thd. May 30th

Advertisements