-
Notifications
You must be signed in to change notification settings - Fork 8
/
DifferenceInYears.java
53 lines (44 loc) · 1.4 KB
/
DifferenceInYears.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
48
49
50
51
52
public class MyDate {
private int day;
private int month;
private int year;
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public String toString() {
return this.day + "." + this.month + "." + this.year;
}
public boolean earlier(MyDate compared) {
if (this.year < compared.year) {
return true;
}
if (this.year == compared.year && this.month < compared.month) {
return true;
}
if (this.year == compared.year && this.month == compared.month
&& this.day < compared.day) {
return true;
}
return false;
}
public int differenceInYears(MyDate comparedDate){
if(!this.earlier(comparedDate)){
int difference = this.year - comparedDate.year;
if(this.month < comparedDate.month ||
this.month == comparedDate.month && this.day < comparedDate.day){
difference--;
}
return difference;
}
else{
int difference = comparedDate.year - this.year;
if(comparedDate.month < this.month ||
this.month == comparedDate.month && comparedDate.day < this.day){
difference--;
}
return difference;
}
}
}