Java program to get birth dates and find who is older

In this program, you will learn how to java program check whether Your friend is older or you. We will use the LocalDate method to calculate your age. Then we will compare your age(day, month, year) with your friend’s age.

How the program will work?

  1. First of all, we will calculate your and your friend’s age.
  2. If your age is greater than your friend then print “You are older”.
  3. If your friend age is greater than your then print “Your friend is older”
  4. Same step 2 and 3 is check for months and date.

Lets write this logic in a Java Program.

Example:

Yourage=18-10-1992
Yourfriendage=18-10-1992
if (Yourage > Yourfriendage)
You are older !
else if (Yourage < Yourfriendage)
Your Friend is older !
else if (Yourage==Yourfriendage)
You and your friend has born on same day!

Example: Program to get birth dates and find who is older

In this program, we have used a scanner to read the birth date and the program check whether your age is greater than your friend or not. To understand this program you should have basic knowledge of LocalDate and if-else-if statement in Core java programming.

import java.time.LocalDate;
import java.time.Period;
import java.util.Scanner;

public class find_who_is_older {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);

        //Your birth date
        System.out.println("Enter Your birth date:");
        System.out.print("date: ");
        int urDate = sc.nextInt();
        System.out.print("Month: ");
        int urMonth = sc.nextInt();
        System.out.print("Year: ");
        int urYear = sc.nextInt();
        LocalDate l = LocalDate.of(urYear, urMonth, urDate);
        LocalDate now = LocalDate.now();
        Period diff = Period.between(l, now);

        //Friend birth date
        System.out.println("Enter Your Friend birth date:");
        System.out.print("date: ");
        int fDate = sc.nextInt();
        System.out.print("Month: ");
        int fMonth = sc.nextInt();
        System.out.print("Year: ");
        int fYear = sc.nextInt();

        LocalDate l1 = LocalDate.of(fYear, fMonth, fDate);
        Period diff1 = Period.between(l1, now);

        if(diff.getYears()>diff1.getYears()){
            System.out.println("You are older !");
        }
        else if(diff.getYears()<diff1.getYears()){
            System.out.println("Your Friend is older !");
        }
        else if(diff.getYears() == diff1.getYears()){

            if(diff.getMonths()>diff1.getMonths()){
                System.out.println("You are older !");
            }
            else if(diff.getMonths()<diff1.getMonths()){
                System.out.println("Your Friend is older !");
            }
            else if(diff.getMonths() == diff1.getMonths()){

                if(diff.getDays()>diff1.getDays()){
                    System.out.println("You are older !");
                }
                else if(diff.getDays()<diff1.getDays()){
                    System.out.println("Your Friend is older !");
                }
                else if(diff.getDays() == diff1.getDays()){
                    System.out.println("You and your friend has born on same day!");
                }

            }

        }

    }

Output:

Enter Your birth date:
date: 22
Month: 11
Year: 1998
Enter Your Friend birth date:
date: 3
Month: 1
Year: 1987
Your Friend is older !

Recommended:

  1. Java Program to Add Two Integers
  2. Java program to convert Hexadecimal to octal number?
  3. How do you reverse a number without a loop in Java?
  4. Java program to check if the number is greater than 100 or not

Leave a Comment

Your email address will not be published. Required fields are marked *

x