-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateOfBirth.java
More file actions
71 lines (44 loc) · 1.5 KB
/
dateOfBirth.java
File metadata and controls
71 lines (44 loc) · 1.5 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package javaC5Q2;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class dateOfBirth {
public int DateOfBirth(String date)throws InvalidAgeException{
try {
DateTimeFormatter dateTime = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localDate = LocalDate.parse(date, dateTime);
LocalDate localDate1 = LocalDate.now();
Period per = Period.between(localDate, localDate1);
if(per.getYears() > 0 || per.getMonths() >0 || per.getDays() >0) {
return per.getYears();
}else {
return -1;
}
}catch(Exception ex) {
InvalidAgeException ae = new InvalidAgeException("please pass the date in proper format");
throw ae;
}
}
public static void main(String[] args) throws InvalidAgeException{
try {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the date of birth in format : dd/MM/yyyy");
String date = sc.next();
dateOfBirth dob = new dateOfBirth();
int Age = dob.DateOfBirth(date);
if(Age > 18) {
System.out.println("you are eligible to vote");
}
if(Age == 18) {
System.out.println("Happy Birthday , you are eligilbe to vote");
}
if(Age < 0) {
System.out.println("Date should not be in future");
}
}catch(Exception ex) {
System.out.println(ex.getMessage());
//ex.printStackTrace();
}
}
}