Compare the Date of Births and find who is older in Java

In real-time projects, sometimes we need to compare the age of two persons and we have to find that who is older. So in this post, we will discuss how we can compare the Date of Births and how can we find who is older among them.

Simple Logic

First, we will accept both Dates of Births of you and your friend and then we will compare both Dates of Births and then we will compare both persons Age or Date of Birth. Finally, we can find who is older from the Date of Births of both persons.

Java Program to compare the date of births and find who is older one

In this program, we have used the compareTo( ) method to compare two birth dates of you and your friend.

The compareTo( ) method of the Date class basically returns a positive value when the first date comes after the second date, returns a negative value if the first date comes before the second date, and returns ‘0’ when both dates will be equal.

After getting the specified values from the compareTo( ) method, we can able to find that who is older among you and your friend. Let’s see the program now.

/** * Compare the Date of Births and find who is older in Java. */ package cf.java.logical; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class CompareTheDataOfBirths { public static void main(String[] args) { Date yourDOB, friendDOB; // Use SimpleDateFormat to format user given Date to a specified format SimpleDateFormat simDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Scanner sc = new Scanner(System.in); try { // Accept Your Date of Birth System.out.print("Enter your own Date of Birth in this YYYY-MM-DD format: "); yourDOB = simDateFormat.parse(sc.nextLine()); // Accept your friend's Date of Birth System.out.print("Enter your friend's Date of Birth ni this YYYY-MM-DD format: "); friendDOB = simDateFormat.parse(sc.nextLine()); // Display both Date of Births System.out.println("My DOB is "+simDateFormat.format(yourDOB)+" and my friend DOB is "+simDateFormat.format(friendDOB)); // Now check who is older among you and your friend if (yourDOB.compareTo(friendDOB) < 0) { System.out.println("Me is older than my friend"); } else if (yourDOB.compareTo(friendDOB) > 0) { System.out.println("My friend is older than me"); } else if (yourDOB.compareTo(friendDOB) == 0) { System.out.println("We both have same age"); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Code language: JavaScript (javascript)

Output for above program

Enter your Date of Birth (In YYYY-MM-DD format): 1995-12-15 Enter your friend's Date of Birth (In YYYY-MM-DD format): 1995-08-12 My DOB is 1995-12-15 and my friend DOB is 1995-08-12 My friend is older than me

FAQs

How to compare two dates in Java?

We can compare two dates by using the compareTo( ) method.
The compareTo( ) method of the Date class basically returns a positive value when the first date comes after the second date, returns a negative value if the first date comes before the second date, and returns ‘0’ when both dates will be equal.

How does the compareTo method work in Java?

compareTo( ) method of Date class compares two Dates for ordering and returns a value which may be ‘0’ or less than 0 or maybe greater than 0 also.

Recommended Posts

Leave a Comment