-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.java
More file actions
162 lines (119 loc) · 2.46 KB
/
Date.java
File metadata and controls
162 lines (119 loc) · 2.46 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
159
160
161
//the Date class, creates objects with dates and increases and decreases date
public class Date{
private int month, day, year;
//Array holds the max number of days in each month
private int[] array = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//string array holds the name of each month
private String[] monthName = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//no argument constructor
public Date(){
month =1;
day = 1;
year= 1980;
}
//constructor takes in date from the user and sets the date, is invalid date, sets date to 1/1/1980
public Date(int mo, int da, int ye){
boolean isDate =false;
//checks to see if the year is a leapyear
if (isLeapYear(ye))
array[1] =29;
else
array[1] = 28;
//if the month, date and year are valid
if(month<=12&&month>=1){
if(day>=1&&day<=array[mo - 1]){
if(year>0)
isDate = true;
}
}
if(isDate){
month = mo;
day = da;
year = ye;
}
else{
month = 1;
day = 1;
year = 1980;
}
}
//thrid constructor takes a date object and creates new object with the same date
public Date(Date d1){
month = d1.getMonth();
day = d1.getDay();
year = d1.getYear();
}
//inceases day by one
public void increaseDay(){
if (isLeapYear(year))
array[1] =29;
else
array[1] = 28;
day++;
if(day > array[month-1]){
day =1;
month++;
if(month>12){
month = 1;
year++;
}
}
}
public String toString(){
return monthName[month -1] + " "+day+", "+ year;
}
//decreases day by one
public void decreaseDay(){
if (isLeapYear(year))
array[1] =29;
else
array[1] = 28;
day--;
if(day==0){
month--;
if(month==0){
month =12;
day = array[month -1];
year--;
}
day = array[month -1];
}
}
public boolean setDate(int moth,int dy, int yr){
boolean isDate =false;
if (isLeapYear(yr))
array[1] =29;
else
array[1] = 28;
if(moth<=12&&moth>=1){
if(dy>=1&&dy<=array[moth - 1]){
if(yr>0)
isDate = true;
}
}
if(isDate){
month = moth;
day = dy;
year = yr;
return true;
}
else
return false;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
public int getYear(){
return year;
}
//checks year to see if it's a leap year
public boolean isLeapYear(int yer){
if((yer % 400==0)||(yer%4==0 && yer%100!=0 ))
return true;
else
return false;
}
}