forked from dharmanshu1921/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAge_Calculator.java
More file actions
Latest commit
35 lines (35 loc) · 1.14 KB
/
Copy pathAge_Calculator.java
File metadata and controls
35 lines (35 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//the program works on Java 8 and later versions
importjava.time.LocalDate;
importjava.time.Period;
importjava.util.Scanner;
publicclassAgeCalculatorExample1
{
publicstaticvoidmain(Stringargs[])
{
System.out.print("Enter date of birth in YYYY-MM-DD format: ");
Scannerscanner = newScanner(System.in);
//reads the date of birth from the user
Stringinput = scanner.nextLine();
scanner.close();
//the parse() method obtains an instance of LocalDate from a text string such as 1992-08-11
LocalDatedob = LocalDate.parse(input);
//prints the age
System.out.println("You are " + calculateAge(dob)+" years old.");
}
//the method calculates the age
publicstaticintcalculateAge(LocalDatedob)
{
//creating an instance of the LocalDate class and invoking the now() method
//now() method obtains the current date from the system clock in the default time zone
LocalDatecurDate = LocalDate.now();
//calculates the amount of time between two dates and returns the years
if ((dob != null) && (curDate != null))
{
returnPeriod.between(dob, curDate).getYears();
}
else
{
return0;
}
}
}