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;
|
}
|
}
|