-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyDate.java
More file actions
158 lines (118 loc) · 2.87 KB
/
Copy pathMyDate.java
File metadata and controls
158 lines (118 loc) · 2.87 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
public class MyDate {
int day;
int month; // should be incremented by one before printing
int year;
int[] maxDays = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month-1; //0 represents January 11 represents December
this.year = year;
}
public void incrementDay() {
int newDay = day+1;
if (newDay > maxDays[month]){
day =1;
incrementMonth();
}else if(month == 1 && newDay ==29 && !inLeapYear()){
day =1;
incrementMonth(); //will also handle the change in year
}else{
day = newDay;
}
}
public void decrementDay() {
int newDay = day - 1;
if (newDay == 0){
day = 31;
decrementMonth(); //will also adjust the day to its max and handle the change in year
}else{
day = newDay;
}
}
public void incrementDay(int i) {
while (i > 0){
incrementDay();
i--;
}
}
public void decrementDay(int i) {
while (i > 0){
decrementDay();
i--;
}
}
public void incrementMonth(int i) {
int newMonth = (month + i) % 12;
int yearChange = 0;
if (newMonth < 0) {
newMonth += 12;
yearChange = -1;
}
yearChange += (month + i) / 12;
month = newMonth;
year += yearChange;
if (day > maxDays[month]){
day = maxDays[month];
if(month ==1 && day ==29 && !inLeapYear()){
day =28;
}
}
}
public void incrementMonth() {
incrementMonth(1);
}
public void decrementMonth(int i) {
incrementMonth(-i);
}
public void decrementMonth() {
incrementMonth(-1);
}
public void incrementYear(int i) {
year+=i;
if (month ==1 && day ==29 && !inLeapYear()){
day = 28;
}
}
public void incrementYear() {
incrementYear(1);
}
public void decrementYear() {
incrementYear(-1);
}
public void decrementYear(int i) {
incrementYear(-i);
}
public boolean isBefore(MyDate anotherDate) {
int a = Integer.parseInt(toString().replaceAll("-", "")) ;
int b = Integer.parseInt(anotherDate.toString().replaceAll("-", ""));
return a < b;
}
public int dayDifference(MyDate anotherDate) {
int diff = 0;
if (isBefore(anotherDate)){
MyDate date = new MyDate(day,month+1,year); //a copy of current object
while (date.isBefore(anotherDate)){
date.incrementDay();
diff++;
}
}else if(isAfter(anotherDate)){
MyDate date = new MyDate(day,month+1,year); // a copy of current object
while (date.isAfter(anotherDate)){
date.decrementDay();
diff++;
}
}
return diff;
}
public boolean isAfter(MyDate anotherDate) {
int a = Integer.parseInt(toString().replaceAll("-", "")) ;
int b = Integer.parseInt(anotherDate.toString().replaceAll("-", ""));
return a > b;
}
public String toString(){
return year + "-"+(month + 1 < 10 ? "0" : "") + (month + 1) + "-" +(day < 10 ? "0" : "") + day;
}
public boolean inLeapYear(){
return year % 4 == 0 ? true : false;
}
}