Java Program to Check the birthday

Java Program to Check the birthday

What is  a Calendar class?

The Calendar class in the Java programming language is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, check the birthday year and so on, and for manipulating the calendar fields, such as getting the date of the next week.

In this Article, we will write a program to check the birthday and print Happy Birthday message in Java.

Calendar Class:

The Calendar class also provides a number of methods for adding or subtracting time from a calendar, such as add() and roll(), and for comparing two calendars.

Java Program to Check the birthday and print Happy Birthday message:

Run
import java.util.*;
import java.text.SimpleDateFormat;

public class Main{
    public static void main(String[] args) {
        // Scanner to take input of date
        Scanner scn = new Scanner(System.in);
        System.out.print("Enter your birthday in the format MM/DD/YYYY: ");
        String birthday = scn.nextLine();
        
        // simpledateformat is used for proper formatting of the date
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        sdf.setLenient(false);
        
        // try block for successful execution
        try {
            Date date = sdf.parse(birthday);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            
            // Taking current date
            Calendar today = Calendar.getInstance();
            
            // comparing today's date with the birth date
            if(cal.get(Calendar.MONTH) == today.get(Calendar.MONTH) 
              && cal.get(Calendar.DATE) == today.get(Calendar.DATE))
                // Prints if today is the birthdate
                System.out.println("Happy Birthday!");
            else
                System.out.println("Valid birthday, but not today.");
        } catch (Exception e) {
            System.out.println("Invalid birthday.");
        }
    }
}
Output:

Enter your birthday in the format MM/DD/YYYY: 05/22/2001
Valid birthday, but not today.

Explanation:

This program uses the SimpleDateFormat class to parse the user’s input and check if it is a valid date. The setLenient(false) method is used to ensure that the date is strictly in the format “MM/DD/YYYY“. Then it uses the Calendar class to compare the birthday with the current date. If the input is a valid date and it is the user’s current birthday, the program will print “Happy Birthday!”. If it is valid but not the current birthday, it will print “Valid birthday, but not today.” otherwise, it will print “Invalid birthday.”

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription