CyberSEO Pro plugin for WordPress

How to accept Date of Birth from user in Java – Best 3 Ways

Read Time:5 Minute, 51 Second

In this tutorial, We will learn how to accept Date of Birth from user and calculate the user’s age in Java. We will ask the user to enter their date of birth and from DOB, we will estimate their age also.

We will learn about how to accept Date of Birth from user only. If you are interested to learn about how to calculate the age also then please follow this tutorial How to accept DOB from user and calculate age?

How to accept Date of Birth from user in Java?

We can accept the Date of Birth from the user by asking for DOB through the below methods.

  1. Accept complete Date of Birth as a String and convert it to Date format
  2. Accept Birth Year, Month, Day and convert it to Date format
  3. Accept complete Date of Birth as Date format and format it

1. Accept the complete Date of Birth as a String and convert it to Date format

We can accept the complete Date of Birth of any person as a String variable from the user by using the Scanner class as given below.

Once we accept the date of birth from the user, We can convert that date of birth to Date format by using the parse( ) method of the LocalDate class.

LocalDate dob = LocalDate.parse(userGivenDate);

Let’s see the complete program now:

package com.cf.latest; import java.time.LocalDate; import java.util.Scanner; public class AcceptDOBFromUser { public static void main(String[] args) throws Exception { String userGivenDate; Scanner sc = new Scanner(System.in); System.out.print("Enter your DOB in YYYY-MM-DD format: "); userGivenDate = sc.nextLine(); LocalDate dob = LocalDate.parse(userGivenDate); System.out.println("DOB of user is " + dob); sc.close(); } }
Code language: Java (java)

Output:

Enter your DOB in YYYY-MM-DD format: 1995-05-24 DOB of user is 1995-05-24

Also Read: Java Program To Print Vowels In A String

2. Accept Birth Year, Month & Day and convert it to Date format

We can accept user birth year, birth month and birth day separately one by one and then we can convert those to Date format by using of( ) function of LocalDate class.

Let’s see the complete program:

package com.cf.latest; import java.time.LocalDate; import java.util.Scanner; public class AcceptDOBFromUserOneByOne { public static void main(String[] args) { int birthYear, birthMon, birthDay; Scanner scanner = new Scanner(System.in); System.out.print("Enter your birth year in YYYY format: "); birthYear = scanner.nextInt(); System.out.print("Enter your birth month in MM format: "); birthMon = scanner.nextInt(); System.out.print("Enter your birth day in DD format: "); birthDay = scanner.nextInt(); LocalDate dob = LocalDate.of(birthYear, birthMon, birthDay); System.out.println("DOB of user is " + dob); } }
Code language: Java (java)

Output:

Enter your birth year in YYYY format: 1995 Enter your birth month in MM format: 05 Enter your birth day in DD format: 24 DOB of user is 1995-05-24

3. Accept complete Date of Birth as Date format and format it

We can accept the complete Date of Birth of any person as Date format from the user by using the Date class as given below.

Once we accept the date of birth from the user, We can format that date of birth as per our requirement and will display the DOB to user.

Let’s see the complete program:

package com.cf.latest; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class AcceptDOBFromUserByUsingDateFormat { public static void main(String[] args) throws ParseException { Scanner sc = new Scanner(System.in); // Ask user to enter Date of Birth System.out.print("Enter your DOB in YYYY-MM-DD format: "); // Accept DOB as Date format Date dob = new SimpleDateFormat("yyyy-MM-dd").parse(sc.nextLine()); // Display DOB of user after formatting System.out.println("DOB of user is " + new SimpleDateFormat("yyyy-MM-dd").format(dob)); sc.close(); } }
Code language: JavaScript (javascript)

Output:

Enter your DOB: 1995-08-25 DOB of user is 1995-08-25

Conclusion

In this tutorial we have learned how to accept Date of Birth from User in Java.

We have seen 3 different method to accept the Date of Birth from the User like accept the DOB as a String, accept the birth year, month and date separately and finally accept the DOB as Date format also.

The most simplest format is to accept the DOB from the user as a String and then convert that String to Date format. You can use any of the above methods to accept the Date of Birth as per your requirement.

We hope, you can be able to achieve your requirement and like our tutorial. If our tutorial helped you or if you liked our tutorial, then please share it with others.

Thanks & Happy Learning!

Recommended Articles

  1. How To Extends Multiple Class In Java – Complete Guide 2022
  2. Best 2 Ways To Give Tab Space In Java
  3. Best 2 Ways To Find GCD Of Two Numbers Using Constructor In Java
  4. How To Find Multiples Of A Float Value In Java?
  5. How To Find Power Of A Number In Java – 3 Simple Ways

Other Queries We have Covered

  • How to accept date of birth from user and calculate age in java?
  • How to calculate the age of user in Java?

FAQs

How to convert String date to Date format?

We can convert String date to Date format by using parse( ) method of LocalDate as below:
LocalDate dob = LocalDate.parse(userGivenDate);

How to format Date as YYYY-MM-DD?

We can format Date by using format( ) method of SimpleDateFormat class.

Date dob = new SimpleDateFormat(“yyyy-MM-dd”).parse(userGivenDate);
Date formatDOB = new SimpleDateFormat(“yyyy-MM-dd”).format(dob));

How to get current date in Java?

We can get current date or today’s date by using the now( ) method of LocalDate class as below:
LocalDate currDate = LocalDate.now();

Source: https://codingface.com/how-to-accept-date-of-birth-from-user-in-java/

The Developer’s Advanced Guide to the wp-config.php File Previous post The Developer’s Advanced Guide to the wp-config.php File
Game Development – Education Ecosystem Blog Next post Game Development – Education Ecosystem Blog

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.