package seed.utils; import java.util.Date; import org.joda.time.DateTime; import org.joda.time.Days; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; public class SeedDateUtil { private DateTime dateTime; public SeedDateUtil(){} public SeedDateUtil(Date date){ dateTime = new DateTime(date); } public void defaultDateSetting(Date date){ dateTime = new DateTime(date); } /** * year, month 정보를 전달 받아서 DateTime을 초기화 하는 메소드 * 일/시/분 은 1일/12시/0분으로 기본 설정 합니다. * @param int year 년 * @param int month 월 * */ public void defaultDateSetting(int year, int month){ dateTime = new DateTime(year, month, 1, 12, 0); } public int getYear(){ return dateTime.getYear(); } public int getMonth(){ return dateTime.getMonthOfYear(); } public int getDay(){ return dateTime.getDayOfMonth(); } public int getBeginningDayOfWeek(){ return dateTime.dayOfMonth().withMinimumValue().getDayOfWeek(); } public int getLastDayOfMonth(){ return dateTime.dayOfMonth().withMaximumValue().getDayOfMonth(); } public String getLastDayOfMonthToString(){ int curData= dateTime.dayOfMonth().withMaximumValue().getDayOfMonth(); if(curData<10){ return "0"+curData; }else{ return String.valueOf(curData); } } public int getHour(){ return dateTime.getHourOfDay(); } public int getMinute(){ return dateTime.getMinuteOfHour(); } public String getYearToString(){ int curYear = dateTime.getYear(); return String.valueOf(curYear); } /** * 10이하의 값은 앞에 0을 붙이게 됩니다. * */ public String getMonthToString(){ int curMonth = dateTime.getMonthOfYear(); if(curMonth<10){ return "0"+curMonth; }else{ return String.valueOf(curMonth); } } public String getMonthToStringDefault(){ int curMonth = dateTime.getMonthOfYear(); return String.valueOf(curMonth); } /** * 10이하의 값은 앞에 0을 붙이게 됩니다. * */ public String getDayToString(){ int curDay = dateTime.getDayOfMonth(); if(curDay<10){ return "0"+curDay; }else{ return String.valueOf(curDay); } } public String getDayToStringDefault(){ int curDay = dateTime.getDayOfMonth(); return String.valueOf(curDay); } public String getSimpleDateFormat(String dateFormat){ DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(dateFormat); return dateTime.toString(dateTimeFormat); } public String getSimpleDateFormat(Date date, String dateFormat){ DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(dateFormat); return new DateTime(date).toString(dateTimeFormat); } /** * 전달 받은 Date의 year,month,day에 해당하는 margin만큰 지난 날짜를 반환하는 * 메소드 * */ public Date dayCalForStr(Date date, int margin, String mode){ DateTime calDateTime = new DateTime(date); if(mode.endsWith("year")){ calDateTime.plusYears(margin); }else if(mode.endsWith("month")){ calDateTime.plusMonths(margin); }else{ calDateTime.plusDays(margin); } return calDateTime.toDate(); } /** * 전달 받은 year, month, day의 year,month,day에 해당하는 margin만큰 지난 날짜를 반환하는 * 시/분 은 12시0분으로 자동 셋팅 * 메소드 * */ public Date dayCalForStr(int year, int month, int day, int margin, String mode){ DateTime calDateTime = new DateTime(year, month, day, 12, 0); if(mode.endsWith("year")){ calDateTime = calDateTime.plusYears(margin); }else if(mode.endsWith("month")){ calDateTime = calDateTime.plusMonths(margin); }else{ calDateTime = calDateTime.plusDays(margin); } return calDateTime.toDate(); } /** * 생성시 설정된 date의 year,month,day에 해당하는 margin만큰 지난 날짜를 반환하는 * 메소드 * */ public Date dayCalForStr(int margin, String mode){ DateTime calDateTime = dateTime; if(mode.endsWith("year")){ calDateTime = calDateTime.plusYears(margin); }else if(mode.endsWith("month")){ calDateTime = calDateTime.plusMonths(margin); }else{ calDateTime = calDateTime.plusDays(margin); } return calDateTime.toDate(); } /** * 전달 받은 날짜(targetDate-yyyy-mm-dd)가 현재 날짜 기준으로 checkNum(날짜) 기준안의 글인지 체크 하는 메소드 * checkNum 날짜 보다 더 전의 글은 false 안의 글이면 true * */ public boolean marginCheck(String dateStr, int checkNum){ DateTime checkDateTime = new DateTime(dateStr); Days checkDays = Days.daysBetween(checkDateTime, dateTime); int checkCnt = checkDays.getDays(); if(checkCnt>checkNum){ return false; }else{ return true; } } /** * 전달 받은 날짜(targetDate-yyyy-mm-dd)가 (checkDate-yyyy-mm-dd)기준으로 checkNum(날짜) 기준안의 날짜인지 체크 하는 메소드 * checkNum 날짜 보다 더 전의 날짜는 true 안의 날짜면 false * */ public boolean marginCheck(String checkDate, String targetDate, int checkNum){ DateTime checkDateTime = new DateTime(checkDate); DateTime targetDateTime = new DateTime(targetDate); Days checkDays = Days.daysBetween(checkDateTime, targetDateTime); int checkCnt = checkDays.getDays(); if(checkCnt>checkNum){ return false; }else{ return true; } } /** * 전달 받은 날짜(targetDate yyyy-mm-dd, hour, minute)가 현재 날짜 기준으로 이전인지 이후 인지 체크하는 메소드 * 현재 날짜 보다 이전일 경우 false 이후 이면 true * */ public boolean atferDateCheck(String targetDate, String hour, String minute){ String[] tDate = targetDate.split("-"); if("".equals(hour)){hour = "0";} if("".equals(minute)){minute = "0";} DateTime targetDateTime = new DateTime(Integer.parseInt(tDate[0]), Integer.parseInt(tDate[1]), Integer.parseInt(tDate[2]), Integer.parseInt(hour), Integer.parseInt(minute)); if(dateTime.isAfter(targetDateTime)){ return true; }else{ return false; } } /** * 시작 날짜(startDate yyyy-mm-dd, hour, minute)와 * 종료 날짜(endDate yyyy-mm-dd, hour, minute) 기준으로 * 오늘 날짜가 속해 있을경우 true * 오늘 날짜가 속해 있지 않을 경우 false * */ public boolean innerDateCheck(String startDate, String startHour, String startMinute, String endDate, String endHour, String endMinute){ String[] sDate = startDate.split("-"); String[] eDate = endDate.split("-"); if("".equals(startHour)){startHour = "00";} if("".equals(startMinute)){startMinute = "00";} if("".equals(endHour)){endHour = "23";} if("".equals(endMinute)){endMinute = "59";} DateTime startDateTime = new DateTime(Integer.parseInt(sDate[0]), Integer.parseInt(sDate[1]), Integer.parseInt(sDate[2]), Integer.parseInt(startHour), Integer.parseInt(startMinute)); DateTime endDateTime = new DateTime(Integer.parseInt(eDate[0]), Integer.parseInt(eDate[1]), Integer.parseInt(eDate[2]), Integer.parseInt(endHour), Integer.parseInt(endMinute)); if(dateTime.isAfter(startDateTime) && dateTime.isBefore(endDateTime)){ return true; }else{ return false; } } /** * 넘겨준 year,month,day,time,minute값으로 디비에 저장할 Date함수를 넘겨주는 메소드 * @param int year 년도 * @param int month 월 * @param int day 일 * @param int time 시간 * @param int minute 분 * @return Date * */ public Date dateInString(int year, int month, int day, int time, int minute){ DateTime createDateTime = new DateTime(year, month, day, time, minute); return createDateTime.toDate(); } /** * dateStr 을 format형식으로 받아서 Date로 반환하는 메소드 * DateTimeFormatter 에 맞는 포멧을 넘겨 주면 자동 변환 * */ public Date dateInSimpleDateFormat(String dateStr, String format){ DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(format); DateTime createDateTime = dateTimeFormat.parseDateTime(dateStr); return createDateTime.toDate(); } }