Date:

Share:

How to accept Date of Birth from user in Java?

Related Articles

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

We will learn how to get a date of birth from a user only. If you are interested in learning how to calculate age, please follow this tutorial How to get DOB from user and calculate age?

How to get a date of birth from a user in Java?

We can get the date of birth from the user by requesting DOB using the below methods.

  1. Get the full date of birth as a string and convert it to a date format
  2. Get birth year, month, day and convert them to date format
  3. Get the full date of birth as a date format and format it

1. Get the full date of birth as a string and convert it to date format

We can get each person’s full date of birth as a string variable from the user through the scanner status as given below.

Once we get the date of birth from the user, we can convert that date of birth into a date format by using analyze ( ) The method of LocalDate class.

LocalDate dob = LocalDate.parse(userGivenDate);

Come see the full 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)

Productivity:

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

Also read: A Java program for printing vowels in a string

2. Get birth year, month and day and convert them to date format

We can get a user Year of Birth, birth month and Birthday individually one by one and then we can convert them to date format by using of ( ) function of LocalDate class.

Let’s see the full 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)

Productivity:

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. Get the full date of birth as a date format and format it

We can get each person’s full date of birth approx Date format from the user through the date class as given below.

Once we get the date of birth from the user, we can format the date of birth as per our requirement and present the DOB to the user.

Let’s see the full 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); System.out.print("Enter your DOB in YYYY-MM-DD format: "); Date dob = new SimpleDateFormat("yyyy-MM-dd").parse(sc.nextLine()); System.out.println("DOB of user is " + new SimpleDateFormat("yyyy-MM-dd").format(dob)); sc.close(); } }

Code language: JavaScript (javascript)

Productivity:

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

Summary

In this guide we learned How to get a date of birth from a user in Java.

We have seen 3 different methods to get the date of birth from the user such as getting the DOB as a string, getting the birth year, month and date separately and finally also getting the DOB as a date format.

The simplest format is to get the DOB from the user as a string and then convert that string to a date format. You can use any of the above methods to get the date of birth as per your requirement.

We hope you can achieve your requirement and love our tutorial. If our guide helped you or if you liked our guide, please share it with others.

Thanks and happy learning!

Recommended articles

  1. How to Extend Multiple Classes in Java – Complete Guide 2022
  2. The 2 best ways to space a tab in Java
  3. 2 Best 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 the power of a number in Java – 3 simple ways

Other queries we have covered

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

Common questions

How to convert date string to date format?

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

How to format date as YYYY-MM-DD?

We can format Date using the format( ) method of the 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 the current date or today’s date by using the now( ) method of the LocalDate class as below:
LocalDate currDate = LocalDate.now();

Source

Popular Articles