-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTime.java
More file actions
183 lines (153 loc) · 5.08 KB
/
Time.java
File metadata and controls
183 lines (153 loc) · 5.08 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package Moblima;
import java.util.*;
class Time {
private Integer year;
private Integer month;
private Integer day;
private Integer hour;
private Integer minute;
private boolean weekday;
private boolean publicHoliday;
private static final String strMonth[] = {"JAN", "FEB", "MAR",
"APR", "MAY", "JUN",
"JUL", "AUG", "SEP",
"OCT", "NOV", "DEC"};
private static ArrayList<String> weekendList = new ArrayList<String>(); // format: yyyymmdd
public Time(String time) {
String[] value = time.split(" ");
year = Integer.parseInt(value[0]);
month = Integer.parseInt(value[1]);
day = Integer.parseInt(value[2]);
hour = Integer.parseInt(value[3]);
minute = Integer.parseInt(value[4]);
updateWeekend();
publicHoliday = false;
if (value[5].compareTo("true") == 0)
publicHoliday = true;
}
public Time() {}
public Time(Integer year, Integer month, Integer day) {
this.setDay(day);
this.setMonth(month);
this.setYear(year);
this.updateWeekend();
}
public static Time manualNewATime() {
Scanner sc = new Scanner(System.in);
Time newTime = new Time();
System.out.println("Please enter year: ");
newTime.setYear(sc.nextInt());
System.out.println("Please enter month: 1~12:");
newTime.setMonth(sc.nextInt());
System.out.println("Please enter day:");
newTime.setDay(sc.nextInt());
System.out.println("Please enter hour: 0~23");
newTime.setHour(sc.nextInt());
System.out.println("Please enter minute: 0~59");
newTime.setMinute(sc.nextInt());
newTime.updateWeekend();
return newTime;
}
public Time getCurrentTime() {
year = Calendar.getInstance().get(Calendar.YEAR);
month = Calendar.getInstance().get(Calendar.MONTH) + 1;
this.day = Calendar.getInstance().get(Calendar.DATE);
this.hour = Calendar.getInstance().get(Calendar.HOUR);
this.minute = Calendar.getInstance().get(Calendar.MINUTE);
this.setWeekday();
publicHoliday = false;
return this;
}
public void printTime() {
System.out.format("%2d %s %4d\n %2d:%2d", day,
strMonth[month - 1], year, hour, minute);
}
public String getStr() {
String str = String.format("%2d %s %4d\n %2d:%2d", day,
strMonth[month - 1], year, hour, minute);
return str;
}
public String toString() {
return String.format("%d %d %d %d %d %s", year, month, day, hour, minute, String.valueOf(publicHoliday));
}
public Integer toInt() {
return this.year * 10000 + this.month * 100 + this.day;
}
public boolean setYear(Integer year) {
this.year = year;
return true;
}
public Integer getYear() {
return year;
}
public boolean setMonth(Integer month) {
this.month = month;
return true;
}
public Integer getMonth() {
return month;
}
public boolean setDay(Integer day) {
this.day = day;
return true;
}
public Integer getDay() {
return day;
}
public boolean setHour(Integer hour) {
this.hour = hour;
return true;
}
public Integer getHour() {
return hour;
}
public boolean setMinute(Integer minute) {
this.minute = minute;
return true;
}
public Integer getMinute() {
return minute;
}
//FIX HERE!!!!
//=================================
public boolean setWeekday() {
this.weekday = false;
return true;
}
public boolean getWeekday() {
return weekday;
}
public void updateWeekend() {
Calendar cal = new GregorianCalendar(year, month - 1, day);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
this.weekday = true;
if (Calendar.SUNDAY == dayOfWeek || Calendar.SATURDAY == dayOfWeek) {
this.weekday = false;
}
}
public String toFormat() {
String ph = "";
if (this.publicHoliday)
ph = "PH";
String weekDay = "";
Calendar cal = new GregorianCalendar(year, month - 1, day);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
switch (dayOfWeek - 1) {
case 0 : weekDay = "SUN";
case 1 : weekDay = "MON";
case 2 : weekDay = "TUE";
case 3 : weekDay = "WED";
case 4 : weekDay = "THU";
case 5 : weekDay = "FRI";
case 6 : weekDay = "SAT";
}
return (this.year + "-" + this.month + "-" + this.day + " " + weekDay + " " + this.hour + ":" + this.minute + " " + ph);
}
public int compareTo(Time t) {
if (this.toInt() > t.toInt())
return 1;
if (this.toInt() == t.toInt())
return 0;
return -1;
}
}