-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.java
More file actions
50 lines (48 loc) · 1.72 KB
/
Copy pathDate.java
File metadata and controls
50 lines (48 loc) · 1.72 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
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Date{
private final int day;
private final int month;
private final int year;
public Date(String day, String month, String year){
this.day = Integer.parseInt(day);
this.month = Integer.parseInt(month);
this.year = Integer.parseInt(year);
}
Calendar calendar = new GregorianCalendar();
public boolean isValidDate(){
if(this.month >= 1 && this.month <= 12 && this.year >= 1000 && this.year < 2023){
if((this.month < 7 && this.month % 2 != 0) || (this.month > 7 && this.month % 2 == 0)){
return this.day >= 1 && this.day <= 31;
}
else if(this.month == 2 && (this.year % 4 == 0 || (this.year % 100 == 0 && this.year % 400 == 0))){
return this.day >= 1 && this.day <= 29;
}
else if(this.month == 2 && !(this.year % 100 == 0 && this.year % 400 == 0)){
return this.day >= 1 && this.day <= 28;
}
else{
return this.day >= 1 && this.day <= 30;
}
}
else{
return false;
}
}
public int ageCount(){
if(calendar.get(Calendar.MONTH) - this.month+1 > 0){
return calendar.get(Calendar.YEAR) - this.year;
}
else if(calendar.get(Calendar.MONTH) - this.month+1 == 0){
if(calendar.get(Calendar.DAY_OF_MONTH) - this.day >= 0){
return calendar.get(Calendar.YEAR) - this.year;
}
else{
return calendar.get(Calendar.YEAR) - this.year - 1;
}
}
else{
return calendar.get(Calendar.YEAR) - this.year - 1;
}
}
}