forked from singh-anant/HacktoberFest22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAge_Calculator.java
47 lines (41 loc) · 1.35 KB
/
Age_Calculator.java
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
36
37
38
39
40
41
42
43
44
45
46
47
// Program Written by minhaj-313
// Calculate Your age from date of birth in Java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class Age_Calculator
{
public static void main(String[] args) throws Exception
{
System.out.print("Please enter date of birth in YYYY-MM-DD: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
scanner.close();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar dob = Calendar.getInstance();
dob.setTime(sdf.parse(input));
System.out.println("Age is:" + getAge(dob));
}
public static int getAge(Calendar dob) throws Exception
{
Calendar today = Calendar.getInstance();
int curYear = today.get(Calendar.YEAR);
int dobYear = dob.get(Calendar.YEAR);
int age = curYear - dobYear;
int curMonth = today.get(Calendar.MONTH);
int dobMonth = dob.get(Calendar.MONTH);
if (dobMonth > curMonth)
{
age--;
} else if (dobMonth == curMonth)
{
int curDay = today.get(Calendar.DAY_OF_MONTH);
int dobDay = dob.get(Calendar.DAY_OF_MONTH);
if (dobDay > curDay)
{
age--;
}
}
return age;
}
}