package com.bb.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class DateTimeUtils {
	private static final long TIMEOUT = 1000 * 20;
	@SuppressWarnings("unused")
	private static final SimpleDateFormat FORMAT_YYYYMMDDHHMMSS = new SimpleDateFormat("yyyyMMddHHmmss");

	public static long getValidDateTime() {
		return System.currentTimeMillis() + TIMEOUT;
	}

	public static long getValidDateTime(long timeout) {
		return System.currentTimeMillis() + timeout;
	}

	public static Date now(long time) {
		return new Date(time);
	}

	public static boolean isBefore(Date date){
		return date.before(new Date());
	}
	
	/**
	 *  오라클DB의 DATE형식 객체 생성(현재 년월일시간분초)
	 *  @return Timestamp
	 */
	public static Timestamp getCurrentTimestamp() {
		Calendar calendar = Calendar.getInstance();
		
		return new Timestamp(calendar.getTime().getTime());
	}
	
	/**
	 *  현재 날짜와 시각을  yyyyMMdd 형태로 반환한다.
	 *  @return String
	 */
	public static String getYyyymmdd() {
		Calendar calendar = Calendar.getInstance();
	    Locale currentLocale = new Locale("KOREAN", "KOREA");
	    String pattern = "yyyyMMdd";
	    SimpleDateFormat formatter = new SimpleDateFormat(pattern, currentLocale);
	    return formatter.format(calendar.getTime());
	}
	
	public static String getYyyymmdd2() {
		Calendar calendar = Calendar.getInstance();
	    Locale currentLocale = new Locale("KOREAN", "KOREA");
	    String pattern = "yyyy-MM-dd";
	    SimpleDateFormat formatter = new SimpleDateFormat(pattern, currentLocale);
	    return formatter.format(calendar.getTime());
	}
	
	
	/**
	 * 현재 날짜와 시각을 Yyyymmddhhmmss 형태로 반환한다.
	 *
	 * @return
	*/
	public static String getYyyymmddhhmmss() {
		Calendar calendar = Calendar.getInstance();
	    Locale currentLocale = new Locale("KOREAN", "KOREA");
	    String pattern = "yyyyMMddHHmmss";
	    SimpleDateFormat formatter = new SimpleDateFormat(pattern, currentLocale);
	    return formatter.format(calendar.getTime());
	}
	
	public static String getYyyymm() {
		Calendar calendar = Calendar.getInstance();
	    Locale currentLocale = new Locale("KOREAN", "KOREA");
	    String pattern = "yyyy-MM";
	    SimpleDateFormat formatter = new SimpleDateFormat(pattern, currentLocale);
	    return formatter.format(calendar.getTime());
	}
	
	/**
	 * 현재 날짜와 시각을 Yyyymmdd 형태로 반환한다.
	 *
	 * @param cal
	 * @return
	*/
	public static String getYyyymmdd(Calendar cal) {
	     Locale currentLocale = new Locale("KOREAN", "KOREA");
	     String pattern = "yyyy-MM-dd";
	     SimpleDateFormat formatter = new SimpleDateFormat(pattern, currentLocale);
	     return formatter.format(cal.getTime());
	}
	
	public static String getYyyymm(Calendar cal) {
	     Locale currentLocale = new Locale("KOREAN", "KOREA");
	     String pattern = "yyyy-MM";
	     SimpleDateFormat formatter = new SimpleDateFormat(pattern, currentLocale);
	     return formatter.format(cal.getTime());
	}
	
	public static String getYyyymmdd(Calendar cal, String pattern) {
	     Locale currentLocale = new Locale("KOREAN", "KOREA");
	     SimpleDateFormat formatter = new SimpleDateFormat(pattern, currentLocale);
	     return formatter.format(cal.getTime());
	}
	
	/**
	 * getGregorianCalendar
	 *
	 * @param yyyymmdd
	 * @return
	*/
	public static GregorianCalendar getGregorianCalendar(String yyyymmdd) {

	     int yyyy = Integer.parseInt(yyyymmdd.substring(0, 4));
	     int mm = Integer.parseInt(yyyymmdd.substring(4, 6));
	     int dd = Integer.parseInt(yyyymmdd.substring(6));

	     GregorianCalendar calendar = new GregorianCalendar(yyyy, mm - 1, dd, 0, 0, 0);

	     return calendar;

	}
	
	/**
	 *  지정된 플래그에 따라 연도 , 월 , 일자를 연산한다.
     *  - 사용 예
     * String date = DateUtil.getOpDate(java.util.Calendar.DATE , 1, "20080101")
	 *  @return String
	 */
	public static String getOpDate(int field, int amount, String date) {

	     GregorianCalendar calDate = getGregorianCalendar(date);

	     if (field == Calendar.YEAR) {
	         calDate.add(GregorianCalendar.YEAR, amount);
	     } else if (field == Calendar.MONTH) {
	         calDate.add(GregorianCalendar.MONTH, amount);
	     } else {
	         calDate.add(GregorianCalendar.DATE, amount);
	     }

	     return getYyyymmdd(calDate);

	}
	
	public static String getOpDate2(int field, int amount, String date) {

	     GregorianCalendar calDate = getGregorianCalendar(date);

	     if (field == Calendar.YEAR) {
	         calDate.add(GregorianCalendar.YEAR, amount);
	     } else if (field == Calendar.MONTH) {
	         calDate.add(GregorianCalendar.MONTH, amount);
	     } else {
	         calDate.add(GregorianCalendar.DATE, amount);
	     }

	     return getYyyymmdd(calDate, "yyyyMMdd");
	    

	}
	
	/**
	 * 2009-03-10 String날짜변수를  2009-03-10 00:00:00 Timestamp 형식으로 반환한다.
	 *
	 * @param dateStr
	 * @return
	*/
	public static Timestamp replaceTimestamp(String dateStr) {
		if(dateStr == null || dateStr.length() != 10) return null;
		
		String year  = dateStr.substring(0, 4);
		String month = dateStr.substring(5, 7);
		String day   = dateStr.substring(8, 10);
		
		Calendar calendar = Calendar.getInstance();
		
		calendar.set(
			Integer.parseInt(year), 
			Integer.parseInt(month)-1, 
			Integer.parseInt(day), 
			0, 
			0, 
			0
		);
		
		return new Timestamp(calendar.getTime().getTime());
	}
	
	/**
	 * 두 날짜간의 기간(년,월,일) 계산 메소드
	 * 날짜 인자가 하나인 경우 Default today로 처리
	 * (나이 계산에 활용)
	 *
	 * @param fromDate java.util.String (예) YYYYMMDD
	 * @return String[3] - 0:Year, 1:Month, 2:Day
	 */	
	public static int getPeriodBetween(String fromDate) 
	{        
		return getPeriodBetween(fromDate, getYyyymmdd());
	}
	
	/**
	 * 두 날짜간의 기간(년,월,일) 계산 메소드
	 * @param fromDate String (예)"20000710"
	 * @param endDate	 String (예)"20001111"
	 * @return String[3] - 0:Year, 1:Month, 2:Day
	 */	
	public static int getPeriodBetween(String fromDate, String endDate) 
	{        
		// fromDate가 endDate보다 클 경우 swap
		if (fromDate.compareTo(endDate) > 0) 
		{
			String temp;
			temp = fromDate;
			fromDate = endDate;
			endDate = temp;
		}

		int fromYYYY= Integer.parseInt(fromDate.substring(0,4));
		int fromMM  = Integer.parseInt(fromDate.substring(4,6));
		int fromDD  = Integer.parseInt(fromDate.substring(6,8));
		int endYYYY = Integer.parseInt(endDate.substring(0,4)); 
		int endMM   = Integer.parseInt(endDate.substring(4,6)); 
		int endDD   = Integer.parseInt(endDate.substring(6,8));
    
		int iYearCount = endYYYY - fromYYYY;
		int iMonthCount = 0;
		int iDayCount = 0;
		
		if(fromDate.substring(4,8).compareTo(endDate.substring(4,8)) > 0)
		{
			iYearCount--;
			iMonthCount = (12-fromMM) + endMM;			
		}
		else
		{
			//iYearCount = iYearCount;
			iMonthCount = endMM - fromMM;
		}
				
		int lastDate = 0;
		if(fromDD > endDD)
		{
			iMonthCount--;
			lastDate = getDaysOfMonth(fromYYYY, fromMM);
			iDayCount = (lastDate-fromDD) + endDD;
		}
		else
		{
			iDayCount = endDD - fromDD;
		}		
		return iDayCount + lastDate;
	}
	
	/**
	 * 입력한 달의 Day count 를 return
	 *
	 * @param year  as String
	 * @param month as String
	 * @return int	the count of month
	 */  
	public static int getDaysOfMonth (String year, String month) 
	{			
		if(year == null || month == null) return -1;	
		return getDaysOfMonth (Integer.parseInt(year), Integer.parseInt(month)) ;
	}
	
	/**
	 * 입력한 달의 Day count 를 return
	 *
	 * @param year  as int
	 * @param month as int
	 * @return int	the count of month
	 */  
	public static int getDaysOfMonth (int year, int month) 
	{			
		if(month < 1 || month > 12) return -1;	
		
		if(month == 2) 
		{
			if ( ((year%4 == 0) && (year%100 != 0)) || (year%400 == 0) ) 
				return 29;
			else 
				return 28;
		}
		else if(month==4||month==6||month==9||month==11) 
			return 30;
		else 
			return 31;
	}
}
