/*********************************************************
 * 프로그램명 : MDate.java 프로그램설명 : 날짜 관련 util로서 날짜 더하기 작 성 자 : 강 원 중 작 성 일 : 2002. 5.27 최신변경일 : 2003. 5.22
 *********************************************************/
package kr.co.kihyun.util;

import java.util.*;

import kr.co.kihyun.lang.MInteger;

public class MDate {

	Calendar calendar;
	String yy = "";
	String mm = "";
	String dd = "";

	String ho = "";
	String mi = "";
	String se = "";

	public MDate() {
		calendar = Calendar.getInstance();
		setDate();
	}

	private void setDate() {
		yy = MInteger.toString(calendar.get(Calendar.YEAR)); // 년
		mm = MInteger.toString(calendar.get(Calendar.MONTH) + 1);// 월
		dd = MInteger.toString(calendar.get(Calendar.DATE)); // 일
		ho = MInteger.toString(calendar.get(Calendar.HOUR_OF_DAY));// 시간 HOUR_OF_DAY 12시 이후에는 13시로 됨.
		mi = MInteger.toString(calendar.get(Calendar.MINUTE));// 분
		se = MInteger.toString(calendar.get(Calendar.SECOND));// 분
	}

	/*
	 * yyyy-mm-dd hh:mm:ss
	 */
	public String getTotDocDate() {
		return yy + "-" + mm + "-" + dd + " " + ho + ":" + mi + ":" + se;
	}

	/*
	 * yyyy-mm-dd hh:mm:ss
	 */
	public boolean isBigThenStartDate(String date) throws NumberFormatException, NullPointerException {
		int yy, mm, dd, ho, mi, se;
		String[] allDate = date.split(" ");
		String[] tmpDate = allDate[0].split("-");
		String[] tmpTime = allDate[1].split(":");
		yy = Integer.parseInt(tmpDate[0]);
		mm = Integer.parseInt(tmpDate[1]);
		dd = Integer.parseInt(tmpDate[2]);
		ho = Integer.parseInt(tmpTime[0]);
		mi = Integer.parseInt(tmpTime[1]);
		se = Integer.parseInt(tmpTime[2]);

		return isBigThenStartDate(yy, mm, dd, ho, mi, se);
	}

	public boolean isBigThenStartDate(int yy, int mm, int dd) {
		return isBigThenStartDate(yy, mm, dd, 0);
	}

	public boolean isBigThenStartDate(int yy, int mm, int dd, int ho) {
		return isBigThenStartDate(yy, mm, dd, ho, 0);
	}

	public boolean isBigThenStartDate(int yy, int mm, int dd, int ho, int mi) {
		return isBigThenStartDate(yy, mm, dd, ho, mi, 0);
	}

	public boolean isBigThenStartDate(int yy, int mm, int dd, int ho, int mi, int se) {
		Calendar tmpCalendar = Calendar.getInstance();
		tmpCalendar.set(yy, mm - 1, dd, ho, mi, se);

		if ((calendar.getTime()).getTime() > (tmpCalendar.getTime()).getTime()) {
			return true;
		}

		return false;
	}

	/*
	 * yyyy-mm-dd hh:mm:ss => yy/mm/dd
	 */
	public static String simpleDate(String date) {
		if (date == null || date.trim().equals("")) {
			return "";
		}

		if (date.length() == 19) {
			String date1 = date.substring(2, 10);
			String date2 = date.substring(11, 19);
			date2 = " (" + date2 + ")";
			return date1 + date2;
		} else if (date.length() == 10) {
			return date.substring(2);
		} else if (date.length() == 8) {
			return date;
		} else {
			return "";
		}
	}

	public String simpleDate2() {
		return simpleDate2(getTotDocDate());
	}

	public static String simpleDate2(String date) {
		if (date == null || date.trim().equals("")) {
			return "";
		}
		return date.substring(0, 10);
	}

	/*
	 * yyyy-mm-dd hh:mm:ss => yyyy-mm-dd (hh:mm:ss)
	 */
	public static String simpleDate3(String date) {
		if (date == null || date.trim().equals("")) {
			return "";
		}

		if (date.length() == 19) {
			String date1 = date.substring(0, 10);
			String date2 = date.substring(11, 19);
			date2 = " (" + date2 + ")";
			return date1 + date2;
		} else if (date.length() == 10) {
			return date.substring(2);
		} else if (date.length() == 8) {
			return date;
		} else {
			return "";
		}
	}

	/*
	 * regurn yyyymmdd
	 */
	public static String currentDate() {
		Date dt = new Date();
		int year = dt.getYear();
		int month = dt.getMonth() + 1;
		int date = dt.getDate();

		String y = "20" + (Integer.toString(year)).substring(1, 3);
		String m = Integer.toString(month);
		String d = Integer.toString(date);

		return y + ((month > 9) ? m : "0" + m) + ((date > 9) ? d : "0" + d);
	}

	/*
	 * regurn yyyymmdd
	 */
	public String currentTime() {
		setDate();
		return getTotDocDate();
	}

	/*
	 * regurn yyyy-mm-dd
	 */
	public static String hyphenCurrentDate() {
		Date dt = new Date();
		int year = dt.getYear();
		int month = dt.getMonth() + 1;
		int date = dt.getDate();

		String y = "20" + (Integer.toString(year)).substring(1, 3);
		String m = Integer.toString(month);
		String d = Integer.toString(date);

		return y + "-" + ((month > 9) ? m : "0" + m) + "-" + ((date > 9) ? d : "0" + d);
	}

	public String getHour() {
		return ho;
	}
}
