+ 3
Pls Help me write a java program that will accept someone date of birth using the current date to give the persons age
Java
3 Antworten
+ 3
https://code.sololearn.com/cYZqn48aWJ51/#java
import java.time.*; // This is used to utilize dates/times
import java.util.Scanner; // This is used to obtain input
public class Program
{
public static void main(String[] args) {
// Get user input: yyyy-mm-dd
Scanner scnr = new Scanner(System.in);
// Store the input as string
String dob = scnr.next();
// Convert string input to date format
LocalDate dateOfBirth = LocalDate.parse(dob);
// Get todays date: yyyy-mm-dd
LocalDate currDate = LocalDate.now();
// Get the difference between the two dates
Period period = Period.between(currDate, dateOfBirth);
// Isolate the number of years to get proper age format
int age = Math.abs(period.getYears());
System.out.println("Date of Birth: " + dob); // Print original input
System.out.println("Today's Date: " + currDate); // Print today's date
System.out.println("You are " + age + " years old!"); // Print their age
}
}
:::::: INPUT :::::::::
1986-01-05
::::: OUTPUT ::::::
Date of Birth: 1986-01-05
Today's Date: 2019-06-24
You are 33 years old!
:::: INPUT :::::
1986-06-25
:::: OUTPUT ::::
Date of Birth: 1986-06-25
Today's Date: 2019-06-24
You are 32 years old!
0
Learnt alot here
- 1
Sorry, but we shouldn't make your homework for you