-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBusinessCalendar.java
191 lines (166 loc) · 6 KB
/
BusinessCalendar.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
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
184
185
186
187
188
189
190
191
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.Properties;
import java.util.TimeZone;
public class BusinessCalendar {
TimeZone tz;
SimpleDateFormat formatter;
ArrayList<Calendar> holidays = new ArrayList();
ArrayList<WorkingHour> sundayWorkingHour;
ArrayList<WorkingHour> mondayWorkingHour;
ArrayList<WorkingHour> tuesdayWorkingHour;
ArrayList<WorkingHour> weddayWorkingHour;
ArrayList<WorkingHour> thursdayWorkingHour;
ArrayList<WorkingHour> fridayWorkingHour;
ArrayList<WorkingHour> satdayWorkingHour;
public BusinessCalendar(Properties properties) throws Exception {
loadBusinessCalendarFromPropertyFile(properties);
}
private class WorkingHour {
public WorkingHour(double from, double to) {
this.from = from;
this.to = to;
}
public double from;
public double to;
}
public Calendar stringToCalendar(String str) throws ParseException {
Calendar cal = new GregorianCalendar();
cal.setTime(formatter.parse(str));
cal.setTimeZone(tz);
return cal;
}
public Calendar today() {
Calendar cal = new GregorianCalendar();
cal.setTimeZone(tz);
return cal;
}
private void loadHolidays(Properties prop) throws ParseException {
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
if (key.startsWith("holiday")) {
Calendar c = stringToCalendar(prop.getProperty(key));
holidays.add(c);
}
}
}
private ArrayList<WorkingHour> loadWorkingHour(String timeExpression) {
if (timeExpression.isEmpty()) {
return null;
}
String[] schedules = timeExpression.split(" \\& ");
ArrayList<WorkingHour> whl = new ArrayList<>();
for (String schedule : schedules) {
String[] time = schedule.split("-");
double fromTime = Double.parseDouble(time[0]);
double toTime = Double.parseDouble(time[1]);
whl.add(new WorkingHour(fromTime, toTime));
}
return whl;
}
private void loadWorkingHours(Properties prop) throws ParseException {
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
if (key.startsWith("weekday")) {
ArrayList<WorkingHour> list = loadWorkingHour(prop.getProperty(key));
switch (key) {
case "weekday.sunday":
sundayWorkingHour = list;
break;
case "weekday.monday":
mondayWorkingHour = list;
break;
case "weekday.tuesday":
tuesdayWorkingHour = list;
break;
case "weekday.wednesday":
weddayWorkingHour = list;
break;
case "weekday.thursday":
thursdayWorkingHour = list;
break;
case "weekday.friday":
fridayWorkingHour = list;
break;
case "weekday.saturday":
satdayWorkingHour = list;
break;
}
}
}
}
private void loadBusinessCalendarFromPropertyFile(Properties prop) throws Exception {
String timeZone = prop.getProperty("calendar.timezone");
if (timeZone == null || timeZone.isEmpty()) {
throw new Exception("calendar.timezone property is not defined");
}
tz = TimeZone.getTimeZone(timeZone);
String dateFormat = prop.getProperty("day.format");
if (dateFormat == null || dateFormat.isEmpty()) {
throw new Exception("day.format property is not defined");
}
formatter = new SimpleDateFormat(dateFormat);
loadHolidays(prop);
loadWorkingHours(prop);
}
private boolean isSameDate(Calendar c1, Calendar c2) {
return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)
&& c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)
&& c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH);
}
private ArrayList<WorkingHour> getWorkingHour(int day_of_week) {
switch (day_of_week) {
case 1:
return sundayWorkingHour;
case 2:
return mondayWorkingHour;
case 3:
return tuesdayWorkingHour;
case 4:
return weddayWorkingHour;
case 5:
return thursdayWorkingHour;
case 6:
return fridayWorkingHour;
case 7:
return satdayWorkingHour;
default:
return null;
}
}
public boolean isHoliday(Calendar date) {
for (Calendar c : holidays) {
if (isSameDate(c, date)) {
return true;
}
}
return false;
}
public boolean isTodayHoliday() {
return isHoliday(today());
}
public boolean isWorkingHour(Calendar c) {
ArrayList<WorkingHour> whl = getWorkingHour(c.get(Calendar.DAY_OF_WEEK));
if (whl == null) {
return false;
}
int matchHour = c.get(Calendar.HOUR_OF_DAY);
int matchMin = c.get(Calendar.MINUTE);
double matchTime = Double.parseDouble(matchHour + "." + matchMin);
for (WorkingHour wh : whl) {
if (matchTime >= wh.from && matchTime <= wh.to) {
return true;
}
}
return false;
}
public boolean isWorkingHourNow() {
return isWorkingHour(today());
}
}