|
@@ -0,0 +1,331 @@
|
|
1
|
+package kr.co.swh.lecture.opensource.icu;
|
|
2
|
+
|
|
3
|
+import java.text.ParseException;
|
|
4
|
+import java.text.SimpleDateFormat;
|
|
5
|
+import java.util.ArrayList;
|
|
6
|
+import java.util.Arrays;
|
|
7
|
+import java.util.Calendar;
|
|
8
|
+import java.util.Date;
|
|
9
|
+import java.util.HashMap;
|
|
10
|
+import java.util.Iterator;
|
|
11
|
+import java.util.List;
|
|
12
|
+import java.util.Map;
|
|
13
|
+import java.util.SortedSet;
|
|
14
|
+import java.util.TreeMap;
|
|
15
|
+import java.util.TreeSet;
|
|
16
|
+
|
|
17
|
+import com.google.common.collect.Lists;
|
|
18
|
+import com.ibm.icu.util.ChineseCalendar;
|
|
19
|
+
|
|
20
|
+import scala.Tuple2;
|
|
21
|
+
|
|
22
|
+public class HolidayUtils {
|
|
23
|
+
|
|
24
|
+ public Map<String, String> solarHolidayMap = new HashMap<String, String>();
|
|
25
|
+ public Map<String, String> lunarHolidayMap = new HashMap<String, String>();
|
|
26
|
+
|
|
27
|
+ public String getDateByString(Date date) {
|
|
28
|
+ return getDateByString(date, "-");
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ public String getDateByString(Date date, String separator) {
|
|
32
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy"+separator+"MM"+separator+"dd");
|
|
33
|
+ return sdf.format(date);
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ /**
|
|
37
|
+ * 양력날짜를 음력날짜로 변환
|
|
38
|
+ * @param 양력날짜 (yyyyMMdd)
|
|
39
|
+ * @return 음력날짜 (yyyyMMdd)
|
|
40
|
+ */
|
|
41
|
+// private String converSolarToLunar(String date) {
|
|
42
|
+// return converSolarToLunar(date, "-");
|
|
43
|
+// }
|
|
44
|
+
|
|
45
|
+ private String converSolarToLunar(String date, String separator) {
|
|
46
|
+ ChineseCalendar cc = new ChineseCalendar();
|
|
47
|
+ Calendar cal = Calendar.getInstance();
|
|
48
|
+
|
|
49
|
+ cal.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
|
|
50
|
+ cal.set(Calendar.MONTH, Integer.parseInt(date.substring(4, 6)) - 1);
|
|
51
|
+ cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date.substring(6)));
|
|
52
|
+
|
|
53
|
+ cc.setTimeInMillis(cal.getTimeInMillis());
|
|
54
|
+
|
|
55
|
+ int y = cc.get(ChineseCalendar.EXTENDED_YEAR) - 2637;
|
|
56
|
+ int m = cc.get(ChineseCalendar.MONTH) + 1;
|
|
57
|
+ int d = cc.get(ChineseCalendar.DAY_OF_MONTH);
|
|
58
|
+
|
|
59
|
+ StringBuffer ret = new StringBuffer();
|
|
60
|
+ ret.append(String.format("%04d", y)).append(separator);
|
|
61
|
+ ret.append(String.format("%02d", m)).append(separator);
|
|
62
|
+ ret.append(String.format("%02d", d));
|
|
63
|
+
|
|
64
|
+ return ret.toString();
|
|
65
|
+ } // end converSolarToLunar
|
|
66
|
+
|
|
67
|
+ public String getDay(String date, int amount) {
|
|
68
|
+ Calendar cal = Calendar.getInstance();
|
|
69
|
+ cal.set(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(4, 6)) - 1, Integer.parseInt(date.substring(6)));
|
|
70
|
+ cal.add(Calendar.DAY_OF_MONTH, amount);
|
|
71
|
+
|
|
72
|
+ return getDateByString(cal.getTime(), "");
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ /**
|
|
76
|
+ * 해당일자가 대체공휴일에 해당하는 지 확인
|
|
77
|
+ * @param 양력날짜 (yyyyMMdd)
|
|
78
|
+ * @return 대체 공휴일이면 true
|
|
79
|
+ */
|
|
80
|
+ private boolean isHolidayAlternate(String date) {
|
|
81
|
+
|
|
82
|
+ String[] altHoliday = new String[] {
|
|
83
|
+ "20150929", "20160210", "20170130", "20180926",
|
|
84
|
+ "20180507", "20190506", "20200127", "20220912",
|
|
85
|
+ "20230124", "20240212", "20240506", "20251008",
|
|
86
|
+ "20270209", "20290924", "20290507"};
|
|
87
|
+
|
|
88
|
+ return Arrays.asList(altHoliday).contains(date);
|
|
89
|
+
|
|
90
|
+ /*
|
|
91
|
+ int year = Integer.parseInt(date.substring(0, 4));
|
|
92
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
|
93
|
+
|
|
94
|
+ // 설날
|
|
95
|
+ String dayFirst2 = convertLunarToSolar(year + "0101");
|
|
96
|
+ String dayFirst3 = convertLunarToSolar(year + "0102");
|
|
97
|
+ String dayFirst1 = String.valueOf(Integer.parseInt(dayFirst2) - 1);
|
|
98
|
+
|
|
99
|
+ // 추석
|
|
100
|
+ String dayThanks1 = convertLunarToSolar(year + "0814");
|
|
101
|
+ String dayThanks2 = convertLunarToSolar(year + "0815");
|
|
102
|
+ String dayThanks3 = convertLunarToSolar(year + "0816");
|
|
103
|
+
|
|
104
|
+ // 어린이날
|
|
105
|
+ String dayChild = year + "0505";
|
|
106
|
+
|
|
107
|
+ // 해당 년도의 대체휴일 목록
|
|
108
|
+ List<String> altHolyday = new ArrayList<String>();
|
|
109
|
+
|
|
110
|
+ if(getDayOfWeek(dayFirst1) == Calendar.SUNDAY || getDayOfWeek(dayFirst2) == Calendar.SUNDAY || getDayOfWeek(dayFirst3) == Calendar.SUNDAY || isHolidaySolar(dayFirst1) || isHolidaySolar(dayFirst2) || isHolidaySolar(dayFirst3)) {
|
|
111
|
+ int y = Integer.parseInt(dayFirst3.substring(0, 4));
|
|
112
|
+ int m = Integer.parseInt(dayFirst3.substring(4, 6)) - 1;
|
|
113
|
+ int d = Integer.parseInt(dayFirst3.substring(6)) + 1;
|
|
114
|
+ Calendar c = Calendar.getInstance();
|
|
115
|
+ c.set(y, m, d);
|
|
116
|
+ altHolyday.add(sdf.format(c.getTime()));
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ if(getDayOfWeek(dayThanks1) == Calendar.SUNDAY || getDayOfWeek(dayThanks2) == Calendar.SUNDAY || getDayOfWeek(dayThanks3) == Calendar.SUNDAY || isHolidaySolar(dayThanks1) || isHolidaySolar(dayThanks2) || isHolidaySolar(dayThanks3)) {
|
|
120
|
+ int y = Integer.parseInt(dayThanks3.substring(0, 4));
|
|
121
|
+ int m = Integer.parseInt(dayThanks3.substring(4, 6)) - 1;
|
|
122
|
+ int d = Integer.parseInt(dayThanks3.substring(6)) + 1;
|
|
123
|
+ Calendar c = Calendar.getInstance();
|
|
124
|
+ c.set(y, m, d);
|
|
125
|
+ altHolyday.add(sdf.format(c.getTime()));
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ int childWeek = getDayOfWeek(dayChild);
|
|
129
|
+
|
|
130
|
+ if(childWeek == Calendar.SATURDAY) {
|
|
131
|
+ int y = Integer.parseInt(dayChild.substring(0, 4));
|
|
132
|
+ int m = Integer.parseInt(dayChild.substring(4, 6)) - 1;
|
|
133
|
+ int d = Integer.parseInt(dayChild.substring(6)) + 2;
|
|
134
|
+ Calendar c = Calendar.getInstance();
|
|
135
|
+ c.set(y, m, d);
|
|
136
|
+ altHolyday.add(sdf.format(c.getTime()));
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ if(childWeek == Calendar.SUNDAY) {
|
|
140
|
+ int y = Integer.parseInt(dayChild.substring(0, 4));
|
|
141
|
+ int m = Integer.parseInt(dayChild.substring(4, 6)) - 1;
|
|
142
|
+ int d = Integer.parseInt(dayChild.substring(6)) + 1;
|
|
143
|
+ Calendar c = Calendar.getInstance();
|
|
144
|
+ c.set(y, m, d);
|
|
145
|
+ altHolyday.add(sdf.format(c.getTime()));
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ return altHolyday.contains(date);
|
|
149
|
+ */
|
|
150
|
+ }
|
|
151
|
+
|
|
152
|
+ public HashMap<String, Tuple2<String, String>> init() {
|
|
153
|
+ HashMap<String, Tuple2<String, String>> holidays = new HashMap<String, Tuple2<String, String>>(); // 선생님이 추가한 코드
|
|
154
|
+ SortedSet<String> sortHoliday = new TreeSet<String>();
|
|
155
|
+ solarHolidayMap.put("0101", "신정"); solarHolidayMap.put("0301", "삼일절");
|
|
156
|
+ solarHolidayMap.put("0505", "어린이날"); solarHolidayMap.put("0606", "현충일");
|
|
157
|
+ solarHolidayMap.put("0815", "광복절"); solarHolidayMap.put("1003", "개천절");
|
|
158
|
+ solarHolidayMap.put("1009", "한글날"); solarHolidayMap.put("1225", "성탄절");
|
|
159
|
+
|
|
160
|
+ lunarHolidayMap.put("0101", "구정"); lunarHolidayMap.put("0102", "구정");
|
|
161
|
+ lunarHolidayMap.put("0408", "석가탄신일"); lunarHolidayMap.put("0814", "추석");
|
|
162
|
+ lunarHolidayMap.put("0815", "추석"); lunarHolidayMap.put("0816", "추석");
|
|
163
|
+
|
|
164
|
+ int year = 2015;
|
|
165
|
+ int endYear = 2030;
|
|
166
|
+
|
|
167
|
+ Calendar c = Calendar.getInstance();
|
|
168
|
+ c.set(year, 0, 1); // 1월 1일부터 시작
|
|
169
|
+
|
|
170
|
+ String solarDate = "";
|
|
171
|
+ String lunarDate = "";
|
|
172
|
+ for(int i=year; i<=endYear;) {
|
|
173
|
+ solarDate = getDateByString(c.getTime(), "");
|
|
174
|
+ lunarDate = converSolarToLunar(solarDate, "");
|
|
175
|
+
|
|
176
|
+ c.add(Calendar.DAY_OF_MONTH, 1);
|
|
177
|
+
|
|
178
|
+ // 대체휴일 체크
|
|
179
|
+ if(isHolidayAlternate(solarDate)) {
|
|
180
|
+ String day = solarDate.substring(0,4) + "-" + solarDate.substring(4,6) + "-" + solarDate.substring(6,8);
|
|
181
|
+ System.out.println(day + " ==> " + "대체공휴일");
|
|
182
|
+ holidays.put(day, new Tuple2<String, String>(day, "대체공휴일")); // 선생님이 추가한 코드
|
|
183
|
+ sortHoliday.add(day);
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ // 양력휴일 체크
|
|
187
|
+ String solarMmdd = solarDate.substring(4,8);
|
|
188
|
+ if(solarHolidayMap.containsKey(solarMmdd)) {
|
|
189
|
+ String day = solarDate.substring(0,4) + "-" + solarDate.substring(4,6) + "-" + solarDate.substring(6,8);
|
|
190
|
+ System.out.println(day + " ==> " + solarHolidayMap.get(solarMmdd));
|
|
191
|
+ holidays.put(day, new Tuple2<String, String>(day, solarHolidayMap.get(solarMmdd))); // 선생님이 추가한 코드
|
|
192
|
+ sortHoliday.add(day);
|
|
193
|
+ }
|
|
194
|
+
|
|
195
|
+ // 음력휴일 체크
|
|
196
|
+ String lunarMmdd = lunarDate.substring(4,8);
|
|
197
|
+ if(lunarHolidayMap.containsKey(lunarMmdd)) {
|
|
198
|
+ // 음력 12월은 마지막날이 29일, 30일 계속 번갈아가면서 있으므로
|
|
199
|
+ // 양력에서 하루를 빼준날이 구정시작하는 날짜이다.
|
|
200
|
+ if(lunarMmdd.equals("0101")) {
|
|
201
|
+ String tmp = getDay(solarDate, -1);
|
|
202
|
+ String day = tmp.substring(0,4) + "-" + tmp.substring(4,6) + "-" + tmp.substring(6,8);
|
|
203
|
+ System.out.println(day + " ==> 구정");
|
|
204
|
+ holidays.put(day, new Tuple2<String, String>(day, "구정")); // 선생님이 추가한 코드
|
|
205
|
+ sortHoliday.add(day);
|
|
206
|
+ }
|
|
207
|
+ String day = solarDate.substring(0,4) + "-" + solarDate.substring(4,6) + "-" + solarDate.substring(6,8);
|
|
208
|
+ System.out.println(day + " ==> " + lunarHolidayMap.get(lunarMmdd));
|
|
209
|
+ holidays.put(day, new Tuple2<String, String>(day, lunarHolidayMap.get(lunarMmdd))); // 선생님이 추가한 코드
|
|
210
|
+ sortHoliday.add(day);
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ year = c.get(Calendar.YEAR);
|
|
214
|
+ if(year != i) {
|
|
215
|
+ i++;
|
|
216
|
+ System.out.println("");
|
|
217
|
+ }
|
|
218
|
+ if(i > endYear) break;
|
|
219
|
+ } // end for_i
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+ // Quiz. 연속된 휴일을 시작과 종료로 연결
|
|
223
|
+
|
|
224
|
+ // holidays에 있는 값 출력
|
|
225
|
+ Iterator<String> holidayKeys = holidays.keySet().iterator();
|
|
226
|
+ while(holidayKeys.hasNext()) {
|
|
227
|
+ String key = holidayKeys.next();
|
|
228
|
+ Tuple2<String, String> tuple = holidays.get(key);
|
|
229
|
+ System.out.println(key + " ~ " + tuple._1() + " ==> " + tuple._2());
|
|
230
|
+ }
|
|
231
|
+ return holidays;
|
|
232
|
+ }
|
|
233
|
+
|
|
234
|
+ public static ArrayList<String> getCurrentWeek(String day) throws ParseException {
|
|
235
|
+ ArrayList<String> list = new ArrayList<String>();
|
|
236
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
237
|
+ Date date = format.parse(day);
|
|
238
|
+
|
|
239
|
+ Calendar calendar = Calendar.getInstance();
|
|
240
|
+ calendar.setTime(date);
|
|
241
|
+ int dayOfWeekDay = calendar.get(Calendar.DAY_OF_WEEK) - calendar.getFirstDayOfWeek();
|
|
242
|
+ calendar.add(Calendar.DAY_OF_MONTH, -dayOfWeekDay);
|
|
243
|
+
|
|
244
|
+ String[] days = {"일","월","화","수","목","금","토"};
|
|
245
|
+ for (int i = 0; i < days.length; i++) {
|
|
246
|
+ list.add(format.format(calendar.getTime()));
|
|
247
|
+ calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
248
|
+ }
|
|
249
|
+ return list;
|
|
250
|
+ }
|
|
251
|
+
|
|
252
|
+ // 종료날짜는 포함하지 않는다.
|
|
253
|
+ public static List<String> getDateListEndDateNotInclude(String startDate, String endDate) throws ParseException{
|
|
254
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
255
|
+ Calendar calendar = Calendar.getInstance();
|
|
256
|
+ calendar.clear();
|
|
257
|
+ List<String> holidayList = Lists.newArrayList();
|
|
258
|
+
|
|
259
|
+ Date sDate = simpleDateFormat.parse(startDate);
|
|
260
|
+ Date eDate = simpleDateFormat.parse(endDate);
|
|
261
|
+
|
|
262
|
+ // 두 날짜의 차이를 계산
|
|
263
|
+ int count = (int)( (eDate.getTime() - sDate.getTime()) / (1000 * 60 * 60 * 24));
|
|
264
|
+ calendar.setTime(sDate);
|
|
265
|
+
|
|
266
|
+ // 차이가 0 보다 큰 경우에는 시작날짜 추가
|
|
267
|
+ if(count >= 0) holidayList.add(startDate);
|
|
268
|
+ // 시작일과 종료일이 1보다 큰 경우에는 날짜를 모두 등록
|
|
269
|
+ for(int dayCount = 1; dayCount <= count; dayCount++) {
|
|
270
|
+ calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
271
|
+ Date time = calendar.getTime();
|
|
272
|
+ String day = simpleDateFormat.format(time);
|
|
273
|
+ holidayList.add(day);
|
|
274
|
+ }
|
|
275
|
+ return holidayList;
|
|
276
|
+ }
|
|
277
|
+
|
|
278
|
+ public static Tuple2<ArrayList<String>, Map<String, Integer>> getStartDayEndDay(List<String> holidays, String startDay, String endDay, String dayOfWeek) throws ParseException {
|
|
279
|
+
|
|
280
|
+ SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
281
|
+ Date startDate = originalFormat.parse(startDay);
|
|
282
|
+ Date endDate = originalFormat.parse(endDay); // 종료일짜를 출석일 제일 마지막 날짜로 초기화
|
|
283
|
+ Calendar calendar = Calendar.getInstance();
|
|
284
|
+ calendar.clear();
|
|
285
|
+
|
|
286
|
+ // 종료 날짜는 출석일 제일 마지막 날짜의 다음 달 말일까지 계산
|
|
287
|
+ calendar.setTime(endDate);
|
|
288
|
+ calendar.add(Calendar.MONTH, 1);
|
|
289
|
+ int monthEndDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
290
|
+ calendar.set(Calendar.DATE, monthEndDay);
|
|
291
|
+ endDate = calendar.getTime();
|
|
292
|
+
|
|
293
|
+ // 시작 날짜는 수강 시작일
|
|
294
|
+ calendar.setTime(startDate);
|
|
295
|
+// 시작 날짜가 포함된 주의 첫번째 날짜 계산
|
|
296
|
+// calendar.setTime(startDate);
|
|
297
|
+// int dayOfWeekDay = calendar.get(Calendar.DAY_OF_WEEK) - calendar.getFirstDayOfWeek();
|
|
298
|
+// calendar.add(Calendar.DAY_OF_MONTH, -dayOfWeekDay);
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+ // 두 날짜의 차이를 계산
|
|
302
|
+ int count = (int)( (endDate.getTime() - calendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
|
|
303
|
+
|
|
304
|
+ SimpleDateFormat mapFormat = new SimpleDateFormat("yyyy년 MM월");
|
|
305
|
+ Map<String, Integer> monthTotalCountMap = new TreeMap<String, Integer>();
|
|
306
|
+ ArrayList<String> subjectDays = new ArrayList<String>();
|
|
307
|
+ for (int i = 0; i <= count; i++) {
|
|
308
|
+ Date time = calendar.getTime();
|
|
309
|
+ String day = originalFormat.format(time);
|
|
310
|
+
|
|
311
|
+ // 과정의 수강요일만 포함하며 휴강일은 카운트에서 제외
|
|
312
|
+ if(dayOfWeek != null && dayOfWeek.charAt(calendar.get(Calendar.DAY_OF_WEEK)-1) == '1' &&
|
|
313
|
+ !holidays.contains(day)){
|
|
314
|
+
|
|
315
|
+ // 해당 월에 대한 총 카운트를 저장
|
|
316
|
+ String monthMap = mapFormat.format(time);
|
|
317
|
+ int monthMapKey = (monthTotalCountMap.containsKey(monthMap)) ? monthTotalCountMap.get(monthMap)+1 : 1;
|
|
318
|
+ monthTotalCountMap.put(monthMap, monthMapKey);
|
|
319
|
+ subjectDays.add(day);
|
|
320
|
+ }
|
|
321
|
+ calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
322
|
+ }
|
|
323
|
+
|
|
324
|
+ return new Tuple2<ArrayList<String>, Map<String,Integer>>(subjectDays, monthTotalCountMap);
|
|
325
|
+ }
|
|
326
|
+
|
|
327
|
+ public static void main(String[] args) {
|
|
328
|
+ HolidayUtils holiday = new HolidayUtils();
|
|
329
|
+ holiday.init();
|
|
330
|
+ }
|
|
331
|
+}
|