OWL ITS + 탐지시스템(인터넷 진흥원)
이민희
2022-03-17 916a3cbabe4e50062fce61ff6f2f5d46c05dfbd1
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
package kr.wisestone.owl.util;
 
import com.google.common.collect.Lists;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.Days;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
public class DateUtil {
 
    public static final String THIS_WEEK = "THIS_WEEK";  //  이번주
    public static final String LAST_WEEK = "LAST_WEEK";    //  지난주
    public static final String LAST_SEVEN_DAYS = "LAST_SEVEN_DAYS";  //  최근 7일
    public static final String THIS_MONTH = "THIS_MONTH";  //  이번달
    public static final String LAST_MONTH = "LAST_MONTH";  //  지난달
    public static final String LAST_THIRTY_DAYS = "LAST_THIRTY_DAYS";  //  최근 30일
    public static final String CUSTOM_INPUT = "CUSTOM_INPUT";   //  직접 입력
 
 
    public static Date convertStrToDate(String source) {
        return convertStrToDate(source, "yyyy-MM-dd HH:mm:ss");
    }
 
    public static Date convertStrToDateOnly(String source) {
        return convertStrToDate(source, "yyyy-MM-dd");
    }
 
    public static Date convertStrToDate(String source, String pattern) {
        return convertStrToDate(source, pattern, Locale.KOREA);
    }
 
    public static Date convertStrToDate(String source, String pattern, Locale locale) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
        try {
            date = sdf.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
 
        return date;
    }
 
    public static String convertDateToYYYYMMDD(Date source) {
        return convertDateToStr(source, "yyyy-MM-dd");
    }
 
    public static String convertDateToHHMMSS(Date source) {
        return convertDateToStr(source, "HH:mm:ss");
    }
 
    public static String convertDateToStr(Date source) {
        return convertDateToStr(source, "yyyy-MM-dd HH:mm:ss");
    }
 
    public static String convertDateToStr(Date source, String pattern) {
        return convertDateToStr(source, pattern, Locale.KOREA);
    }
 
    public static String convertDateToStr(Date source, String pattern, Locale locale) {
        String date = null;
 
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
        date = sdf.format(source);
 
        return date;
    }
 
    public static Date addDays(Date date, int amount) {
        return DateUtils.addDays(date, amount);
    }
 
    public static Integer getDateDiff(Date fromDate, Date toDate) {
 
        return Days.daysBetween(new DateTime(fromDate), new DateTime(toDate)).getDays();
    }
 
    public static boolean isValidDate(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
 
        if (date == null) {
            return false;
        }
        String format = null;
        try {
            format = sdf.format(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.equals(format);
    }
 
    //  검색 날짜를 가져온다.
    public static List<Date> getSearchDays(String condition) {
        Calendar cal = Calendar.getInstance();
        List<Date> days = Lists.newArrayList();
 
        switch(condition) {
            //  최근 7일 날짜 리스트 가져오기
            case DateUtil.LAST_SEVEN_DAYS :
                days.add(cal.getTime());
 
                for (int count = 0; count < 6; count++) {
                    cal.add(Calendar.DATE, -1);
                    days.add(cal.getTime());
                }
                break;
            case DateUtil.THIS_WEEK:
                //  이번주 날짜 리스트 가져오기
                days.add(cal.getTime());
 
                while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                    cal.add(Calendar.DATE, -1);
                    days.add(cal.getTime());
                }
                break;
 
            case DateUtil.LAST_WEEK :
                //  지난주 날짜 리스트 가져오기
                while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                    cal.add(Calendar.DATE, -1);
                }
                cal.add(Calendar.DATE, -1);
                days.add(cal.getTime());
 
                while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                    cal.add(Calendar.DATE, -1);
                    days.add(cal.getTime());
                }
 
                break;
 
            case DateUtil.THIS_MONTH :
                //  이번달 날짜 리스트 가져오기
                days.add(cal.getTime());
                while(!"01".equals(new SimpleDateFormat("dd").format(cal.getTime()))) {
                    cal.add(Calendar.DATE, -1);
                    days.add(cal.getTime());
                }
                break;
            case DateUtil.LAST_MONTH :
                //  지난달 날짜 리스트 가져오기기
                while(!"01".equals(new SimpleDateFormat("dd").format(cal.getTime()))) {
                    cal.add(Calendar.DATE, -1);
                }
 
                cal.add(Calendar.DATE, -1);
                days.add(cal.getTime());
                while(!"01".equals(new SimpleDateFormat("dd").format(cal.getTime()))) {
                    cal.add(Calendar.DATE, -1);
                    days.add(cal.getTime());
                }
 
                break;
            case DateUtil.LAST_THIRTY_DAYS :
                for (int count = 0; count > -30; count-- ) {
                    days.add(DateUtil.addDays(new Date(), count));
                }
 
                break;
        }
 
        //  역순 정렬
        Collections.reverse(days);
 
        return days;
    }
}