-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDifferenceDate.java
56 lines (45 loc) · 1.52 KB
/
DifferenceDate.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
53
54
55
56
import static java.lang.Math.abs;
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) {
// int temp = abs(this.month - comparedDate.month);
if (this.earlier(comparedDate)) {
if (comparedDate.month >= this.month && comparedDate.day >= this.day) {
return comparedDate.year - this.year;
} else {
return comparedDate.year - this.year - 1;
}
} else {
if (this.month >= comparedDate.month && this.day >= comparedDate.day) {
return this.year - comparedDate.year;
} else {
return this.year - comparedDate.year - 1;
}
}
//return this.year - comparedDate.year;
// first.year - second.year OR first.year - second.year - 1
}
}