Skip to content

Commit 8cfd210

Browse files
committed
Add the date format parsing util method
1 parent f671eb1 commit 8cfd210

File tree

2 files changed

+110
-20
lines changed

2 files changed

+110
-20
lines changed

src/main/java/root/utils/DateUtils.java

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,40 @@
44
import java.text.SimpleDateFormat;
55
import java.time.DayOfWeek;
66
import java.time.LocalDate;
7+
import java.time.format.DateTimeFormatter;
78
import java.time.format.TextStyle;
9+
import java.util.ArrayList;
810
import java.util.Calendar;
911
import java.util.Date;
1012
import java.util.GregorianCalendar;
13+
import java.util.List;
1114
import java.util.Locale;
1215

1316
public class DateUtils {
1417

18+
public static List<DateTimeFormatter> DATE_TIME_FORMATTER_LIST = new ArrayList<>();
19+
static {
20+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss yyyy", Locale.ENGLISH));
21+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ofPattern("yyyy/MM/dd", Locale.ENGLISH));
22+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH));
23+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH));
24+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.BASIC_ISO_DATE);
25+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_DATE);
26+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_DATE_TIME);
27+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_INSTANT);
28+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_LOCAL_DATE);
29+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
30+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_LOCAL_TIME);
31+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_OFFSET_DATE);
32+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
33+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_OFFSET_TIME);
34+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_ORDINAL_DATE);
35+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_TIME);
36+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_WEEK_DATE);
37+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.ISO_ZONED_DATE_TIME);
38+
DATE_TIME_FORMATTER_LIST.add(DateTimeFormatter.RFC_1123_DATE_TIME);
39+
}
40+
1541
/**
1642
* 오늘 날짜 및 시간을 지정한 format에 따라 반환한다.
1743
*
@@ -76,11 +102,11 @@ public static boolean isWeekEnd(int year, int month, int day) {
76102
}
77103

78104
/**
79-
* 기준일자에서 년, 월, 일을 더한 날짜를 반환한다.
80-
* 빼기도 가능하다.
81-
* @param curDate 기준일자
82-
* @param returnFormat 반환받을 날짜포맷
83-
* @param year
105+
* 기준일자에서 년, 월, 일을 더한 날짜를 반환한다. 빼기도 가능하다.
106+
*
107+
* @param curDate 기준일자
108+
* @param returnFormat 반환받을 날짜포맷
109+
* @param year
84110
* @param month
85111
* @param day
86112
* @return
@@ -94,12 +120,12 @@ public static String addDate(Date curDate, String returnFormat, int year, int mo
94120
cal.add(Calendar.DATE, day);
95121
return sdf.format(cal.getTime());
96122
}
97-
123+
98124
/**
99-
* 기준일자에서 년, 월, 일을 더한 날짜를 반환한다.
100-
* 빼기도 가능하다.
101-
* @param dateStringYMD yyyy-MM-dd 형태의 날짜 문자열
102-
* @param year
125+
* 기준일자에서 년, 월, 일을 더한 날짜를 반환한다. 빼기도 가능하다.
126+
*
127+
* @param dateStringYMD yyyy-MM-dd 형태의 날짜 문자열
128+
* @param year
103129
* @param month
104130
* @param day
105131
* @return
@@ -117,10 +143,11 @@ public static String addDate(String dateStringYMD, int year, int month, int day)
117143
cal.add(Calendar.DATE, day);
118144
return sdf.format(cal.getTime());
119145
}
120-
146+
121147
/**
122-
* 두 날짜간 차이를 반환한다. (첫번째날짜 - 두번째날짜)
123-
* 이 때, 각 날짜의 포맷은 첫번째 인자로 전달한 dateFormat과 동일해야한다.
148+
* 두 날짜간 차이를 반환한다. (첫번째날짜 - 두번째날짜) 이 때, 각 날짜의 포맷은 첫번째 인자로 전달한 dateFormat과
149+
* 동일해야한다.
150+
*
124151
* @param dateFormat
125152
* @param date1
126153
* @param date2
@@ -130,17 +157,18 @@ public static long getDateDiffTime(String dateFormat, String date1, String date2
130157
long diffTime = 0;
131158
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
132159
try {
133-
Date FirstDate = sdf.parse(date1);
134-
Date SecondDate = sdf.parse(date2);
135-
diffTime = FirstDate.getTime() - SecondDate.getTime();
160+
Date FirstDate = sdf.parse(date1);
161+
Date SecondDate = sdf.parse(date2);
162+
diffTime = FirstDate.getTime() - SecondDate.getTime();
136163
} catch (ParseException e) {
137164
e.printStackTrace();
138165
}
139166
return diffTime / 1000;
140167
}
141-
168+
142169
/**
143170
* 날짜 표현 포맷을 변환한다.
171+
*
144172
* @param fromFormat
145173
* @param toFormat
146174
* @param dateString
@@ -159,7 +187,7 @@ public static String convertDateFormat(String fromFormat, String toFormat, Strin
159187
}
160188
return convertedDateString;
161189
}
162-
190+
163191
/**
164192
* Date 객체를 포맷팅한다.
165193
*
@@ -170,7 +198,15 @@ public static String convertDateFormat(String fromFormat, String toFormat, Strin
170198
public static String format(Date date, String toFormat) {
171199
return new SimpleDateFormat(toFormat).format(date);
172200
}
173-
201+
202+
/**
203+
* 날짜간 대소관계를 비교한다.
204+
*
205+
* @param format 날짜 포맷
206+
* @param base 기준 날짜
207+
* @param compare 비교 날짜
208+
* @return
209+
*/
174210
public static int compareTo(String format, String base, String compare) {
175211
try {
176212
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
@@ -179,9 +215,32 @@ public static int compareTo(String format, String base, String compare) {
179215

180216
return date1.compareTo(date2);
181217
} catch (ParseException e) {
182-
218+
183219
}
184220

185221
return -1;
186222
}
223+
224+
/**
225+
* DateFormat에 대한 정규표현식을 모를 때, 사전 저장된 DateTimeFormatter 목록을 순회하여 날짜문자열을 파싱한다.
226+
*
227+
* @param dateString 날짜 문자열
228+
* @return
229+
*/
230+
public static LocalDate parse(String dateString) {
231+
LocalDate result = null;
232+
233+
for (DateTimeFormatter formatter : DATE_TIME_FORMATTER_LIST) {
234+
try {
235+
result = LocalDate.parse(dateString, formatter);
236+
} catch (Exception e) {
237+
}
238+
239+
if (result != null) {
240+
break;
241+
}
242+
}
243+
244+
return result;
245+
}
187246
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package Utils;
2+
3+
import static org.junit.jupiter.api.Assertions.assertNotNull;
4+
5+
import java.time.LocalDate;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import org.junit.jupiter.api.Test;
10+
11+
import root.utils.DateUtils;
12+
13+
public class DateUtilsTest {
14+
15+
@Test
16+
public void parseTest() {
17+
18+
List<String> dateStringList = new ArrayList<>();
19+
dateStringList.add("2022-01-01T04:42:24.005764+09:00");
20+
dateStringList.add("Fri Apr 01 01:37:34 2022");
21+
dateStringList.add("2022-04-03");
22+
dateStringList.add("2022/04/03");
23+
dateStringList.add("2022-04-03 16:10:01");
24+
dateStringList.add("2022/04/03 16:10:01");
25+
26+
for (String dateString : dateStringList) {
27+
LocalDate date = DateUtils.parse(dateString);
28+
assertNotNull(date);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)