DateUtil2
package kr.co.leecom.lcms.util;
import java.lang.StringBuffer;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.log4j.Logger;
import kr.co.leecom.lcms.util.Lunar;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateBean {
private static Logger logger = Logger.getLogger(TextBean.class);
/**
* 기본생성자
*/
public DateBean() {
}
/**
* 년을 리턴한다.
* @param date String yyyyMMdd format이어야 한다.
* @return String
*/
public String getYear(String date) {
date = removeChar(date, "/");
date = removeChar(date, "-");
date = removeChar(date, ".");
return date.substring(0, 4);
}
/**
* 월을 리턴한다.
* @param date String yyyyMMdd format이어야 한다.
* @return String
*/
public String getMonth(String date) {
date = removeChar(date, "/");
date = removeChar(date, "-");
date = removeChar(date, ".");
return date.substring(4, 6);
}
/**
* 쿼터를 리턴한다.
* @param date String yyyyMMdd format이어야 한다.
* @return String
*/
public String getQuarter(String date) {
date = removeChar(date, "/");
date = removeChar(date, "-");
date = removeChar(date, ".");
if (Integer.parseInt(date.substring(4, 6)) < 4) {
return "1";
} else if (Integer.parseInt(date.substring(4, 6)) < 7) {
return "2";
} else if (Integer.parseInt(date.substring(4, 6)) < 10) {
return "3";
} else if (Integer.parseInt(date.substring(4, 6)) < 13) {
return "4";
}
return "";
}
/**
* 일을 리턴한다.
* @param date String yyyyMMdd format이어야 한다.
* @return String
*/
public String getDay(String date) {
date = removeChar(date, "/");
date = removeChar(date, "-");
date = removeChar(date, ".");
return date.substring(6);
}
/**
* 오늘 날짜를 format의 형태로 리턴한다. <p>
* yyyyMMdd <p>
* yyyy-MM-dd <p>
* yyyy-MM-dd HH:mm:ss <p>
* HHmmss <p>
* HH:mm:ss <p>
* yyyy <p>
* MM <p>
* dd <p>
* yyyyMM <p>
* yyyyMMddHHmmss <p>
* @param format String
* @return String
*/
public String date(String format) {
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(format,java.util.Locale.KOREA);
return formatter.format(new java.util.Date());
}
/**
* 오늘 날짜를 format의 형태로 리턴한다. <p>
* yyyyMMdd <p>
* yyyy-MM-dd <p>
* yyyy-MM-dd HH:mm:ss <p>
* HHmmss <p>
* HH:mm:ss <p>
* yyyy <p>
* MM <p>
* dd <p>
* yyyyMM <p>
* yyyyMMddHHmmss <p>
* @param format String
* @param date Date
* @return String
*/
public String date(String format, java.util.Date date) {
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(format, java.util.Locale.KOREA);
return formatter.format(date);
}
/**
* 특정 날짜를 format의 형태의 일자로 리턴한다. <p>
* yyyyMMdd <p>
* yyyy-MM-dd <p>
* HHmmss <p>
* HH:mm:ss <p>
* yyyy <p>
* MM <p>
* dd <p>
* yyyyMM <p>
* @param format String
* @param date String
* @return String
*/
public String date(String format, String date) {
String result = null;
try {
SimpleDateFormat f = null;
SimpleDateFormat sdf = null;
if (date == null)
return "";
date = date.trim();
date = removeChar(date, "/");
date = removeChar(date, "-");
date = removeChar(date, ".");
if (date.equals(""))
return "";
sdf = new SimpleDateFormat("yyyyMMdd");
f = new SimpleDateFormat(format);
result = f.format(new java.util.Date(sdf.parse(date).getTime()));
}catch (Exception e) {
System.out.println("FormatUtil::date(String format, String date) ERROR : " + e.getMessage());
}
return result;
}
/**
* 특정 날짜에서 년,월,일을 특정수만큼 더한다.
* @param format String
* @param date String
* @param field String
* @param amount int
* @param strTime String
*/
public String addDate(String format, String date, String field,
int amount) {
if (field != null)
field = field.toUpperCase();
SimpleDateFormat fmt = new SimpleDateFormat(format);
Calendar cal = new GregorianCalendar();
int year = 0;
int month = 0;
int day = 0;
if (getYear(date) != null)
year = Integer.parseInt(getYear(date));
if (getMonth(date) != null)
month = Integer.parseInt(getMonth(date));
if (getDay(date) != null)
day = Integer.parseInt(getDay(date));
cal.set(year, month - 1, day);
if (field.equals("YEAR"))
cal.add(Calendar.YEAR, amount);
if (field.equals("MONTH"))
cal.add(Calendar.MONTH, amount);
if (field.equals("DAY"))
cal.add(Calendar.DATE, amount);
String strTime =
fmt.format(new java.util.Date(cal.getTime().getTime()));
return strTime;
}
/**
* 지정된 달의 최초 일자를 리턴한다.
* @param format 리턴되는 날짜의 포멧
* @param date yyyyMMdd 형태의 날짜
* @return String
*/
public String getFirstDateOfMonth(String format, String date) {
SimpleDateFormat fmt = new SimpleDateFormat(format);
Calendar cal = new GregorianCalendar();
int year = 0;
int month = 0;
int day = 0;
int firstDay = 0;
if (getYear(date) != null)
year = Integer.parseInt(getYear(date));
if (getMonth(date) != null)
month = Integer.parseInt(getMonth(date));
if (getDay(date) != null)
day = Integer.parseInt(getDay(date));
cal.set(year, month - 1, day);
firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DATE, firstDay);
String result = fmt.format(new java.util.Date(cal.getTime().getTime()));
return result;
}
/**
* 지정된 달의 마지막 일자를 리턴한다.
* @param format 리턴되는 날짜의 포멧
* @param date yyyyMMdd 형태의 날짜
* @return String
*/
public String getLastDateOfMonth(String format, String date) {
SimpleDateFormat fmt = new SimpleDateFormat(format);
Calendar cal = new GregorianCalendar();
int year = 0;
int month = 0;
int day = 0;
int lastDay= 0;
if (getYear(date) != null)
year = Integer.parseInt(getYear(date));
if (getMonth(date) != null)
month = Integer.parseInt(getMonth(date));
if (getDay(date) != null)
day = Integer.parseInt(getDay(date));
cal.set(year, month - 1, day);
lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DATE, lastDay);
String result = fmt.format(new java.util.Date(cal.getTime().getTime()));
return result;
}
/**
* 이전달의 마지막 일자를 리턴한다.
* @param format 리턴되는 날짜의 포멧
* @return String
*/
public String getLastDateOfBeforeMonth(String format) {
SimpleDateFormat fmt = new SimpleDateFormat(format);
Calendar cal = new GregorianCalendar();
cal.add(Calendar.MONTH, -1);
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DATE, lastDay);
String result = fmt.format(new java.util.Date(cal.getTime().getTime()));
return result;
}
/**
* 지정된 두 날짜 사이의 차이를 구한다.
* @param format 리턴되는 날짜의 포멧
* @param staetdt 시작일자
* @param enddt 종료일자
* @return long
*/
public long getDayDistance( String startdt, String enddt)
throws Exception{
String format = "yyyyMMdd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date sDate;
Date eDate;
startdt = removeChar(startdt, "/");
startdt = removeChar(startdt, "-");
startdt = removeChar(startdt, ".");
enddt = removeChar(enddt, "/");
enddt = removeChar(enddt, "-");
enddt = removeChar(enddt, ".");
long day2day = 0;
try {
sDate = sdf.parse(startdt);
eDate = sdf.parse(enddt);
day2day = (eDate.getTime() - sDate.getTime()) / (1000*60*60*24);
}catch (Exception e) {
throw new Exception ("wrong format string" + e.toString());
}
return Math.abs(day2day);
}
/**
* 지정된 두 날짜 사이의 오늘날자가 포함되어있느지 0사이,
* 1미래, -1과거
* @param startdt 시작일자
* @param enddt 종료일자
* @return String
*/
public String getBetweenDate( String startdt, String enddt) throws Exception {
startdt = removeChar(startdt, "/");
startdt = removeChar(startdt, "-");
startdt = removeChar(startdt, ".");
enddt = removeChar(enddt, "/");
enddt = removeChar(enddt, "-");
enddt = removeChar(enddt, ".");
String curdate = this.date("yyyyMMdd");
if ((Integer.parseInt(curdate, 10) >= Integer.parseInt(startdt, 10)) && (Integer.parseInt(curdate, 10)<=Integer.parseInt(enddt, 10))) {
return "0";
} else if (Integer.parseInt(curdate, 10) < Integer.parseInt(startdt, 10)){
return "1";
} else if (Integer.parseInt(curdate, 10) > Integer.parseInt(enddt, 10)) {
return "-1";
} else {
return "";
}
}
/**
* 지정된 일자의 이전달의 마지막 일자를 리턴한다.
* @param format 리턴되는 날짜의 포멧
* @param date yyyyMMdd 형태의 날짜
* @return String
*/
public String getLastDateOfBeforeMonth(String format, String date) {
SimpleDateFormat fmt = new SimpleDateFormat(format);
Calendar cal = new GregorianCalendar();
int year = 0;
int month = 0;
int day = 0;
int lastDay = 0;
if (getYear(date) != null)
year = Integer.parseInt(getYear(date));
if (getMonth(date) != null)
month = Integer.parseInt(getMonth(date));
if (getDay(date) != null)
day = Integer.parseInt(getDay(date));
cal.set(year, month - 1, day);
cal.add(Calendar.MONTH, -1);
lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DATE, lastDay);
String result = fmt.format(new java.util.Date(cal.getTime().getTime()));
return result;
}
/**
* 현재 index에 대한 요일을 리턴
* @param nWeek int
* @return String sWeekDayKr
*/
public String getWeekDayKr(int nWeek) {
String sWeekDayKr = "";
if (nWeek == 0)
sWeekDayKr = "일";
else if (nWeek == 1)
sWeekDayKr = "월";
else if (nWeek == 2)
sWeekDayKr = "화";
else if (nWeek == 3)
sWeekDayKr = "수";
else if (nWeek == 4)
sWeekDayKr = "목";
else if (nWeek == 5)
sWeekDayKr = "금";
else if (nWeek == 6)
sWeekDayKr = "토";
return sWeekDayKr;
}
/**
* 입력된 일짜의 요일을 리턴한다.
* @param date String 날짜
* @return int getWeekDay
*/
public int getWeekDay(String date) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
Calendar cal = new GregorianCalendar();
int year = 0;
int month = 0;
int day = 0;
if (getYear(date) != null)
year = Integer.parseInt(getYear(date));
if (getMonth(date) != null)
month = Integer.parseInt(getMonth(date));
if (getDay(date) != null)
day = Integer.parseInt(getDay(date));
cal.set(year, month - 1, day);
return cal.get(Calendar.DAY_OF_WEEK) - 1;
}
/**
* TEXT관련 나중에 검사한후 TEXTBEAN으로 옮겼으면...
* @author pdspds
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* String형 str에서 특정 String형 character를 제거한후 리턴
* @param str String 변환한 문자열
* @param character String 제거할 문자
*/
public String removeChar(String str, String character) {
StringTokenizer st = null;
StringBuffer buf = null;
String result = null;
char c;
result = str;
for (int i = 0; i < character.length(); i++) {
c = character.charAt(i);
st = new StringTokenizer(result, String.valueOf(c));
buf = new StringBuffer();
while (st.hasMoreTokens()) {
buf.append(st.nextToken());
}
result = buf.toString();
}
return result;
}
/**
* int형을 스트링으로 변환
* (앞에 "0"을 붙여서 자리수를 맞추어서 리턴)
* @param value int 변환할 숫자
* @param unit int 전체 자릿수
* @return String
*/
public String toUnit(int value, int unit) {
String str = new String();
str = Integer.toString(value);
while (str.length() < unit) {
str = "0" + str;
}
return str;
}
public String toLunar(String date) {
if (date == null)
return "";
date = date.trim();
date = removeChar(date, "/");
date = removeChar(date, "-");
date = removeChar(date, ".");
if (date.equals("") || date.length() < 8)
return "";
int year = 0;
int month = 0;
int day = 0;
if (getYear(date) != null)
year = Integer.parseInt(getYear(date));
if (getMonth(date) != null)
month = Integer.parseInt(getMonth(date));
if (getDay(date) != null)
day = Integer.parseInt(getDay(date));
Lunar lunar = new Lunar();
return lunar.countToDateForLunar(lunar.countSolarDay(year, month, day));
}
public String toSolar(String date) {
if (date == null)
return "";
date = date.trim();
date = removeChar(date, "/");
date = removeChar(date, "-");
date = removeChar(date, ".");
if (date.equals("") || date.length() < 8)
return "";
int year = 0;
int month = 0;
int day = 0;
if (getYear(date) != null)
year = Integer.parseInt(getYear(date));
if (getMonth(date) != null)
month = Integer.parseInt(getMonth(date));
if (getDay(date) != null)
day = Integer.parseInt(getDay(date));
Lunar lunar = new Lunar();
return lunar.countToDateForSolar(lunar.countLunarDay(year, month, day, false));
}
public static String getDateNTimeByForm(String sDateNTimeForm) {
Calendar Today = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat(sDateNTimeForm);
String sDateNTime = sdf.format(Today.getTime() );
return sDateNTime;
}
/**
* 오늘날짜와 비교 몇일인자 Long 반환
*
* @param 비교 날짜 ex)yyyy.MM.dd
* @return 비교 차 ex) 2321
* @throws ParseException
* @throws ParseException
*/
public static long compare2Date(String end)
throws ParseException {
try{
if(end.length() != 8 && end.length() !=14 ){
throw new Exception();
}
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE , cal.get(Calendar.DATE));
DecimalFormat df = new DecimalFormat("00");
Date beginDate = formatter.parse(Integer.toString(cal.get(Calendar.YEAR)) + "-" + df.format(cal.get(Calendar.MONTH)+1) + "-" + df.format(cal.get(Calendar.DATE)));
Date endDate = formatter.parse(end.substring(0,4)+ "-" + end.substring(4,6) + "-" + end.substring(6,8));
return (endDate.getTime() - beginDate.getTime()) /
(60 * 1000);
}catch (Exception e) {
return -40000;
}
}
}
'JAVA' 카테고리의 다른 글
[JAVA]Dispatche :: request.getRequestDispatcher("/prg/board_excel.jsp"); (0) | 2017.11.03 |
---|---|
[JAVA]dbconnection 끊어지는 현상 (0) | 2017.11.03 |
[JAVA]DateUtil (0) | 2017.11.01 |
[JAVA] context root path (0) | 2017.11.01 |
[JAVA] CMYK 이미지 처리 (0) | 2017.11.01 |