package com.nms.server.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.nms.server.common.Constants;

public class DateUtil {
	private static Logger logger = Logger.getLogger(DateUtil.class);
	/**
	 * 处理日期时，用到参数。格式24小时制yyyy-MM-dd HH:mm:ss
	 */
	public static final SimpleDateFormat YYYY_MM_DD_HH24_MM_SS = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	/**
	 * 处理日期时，用到参数。格式为yyyy-MM-dd
	 */
	public static final SimpleDateFormat YYYY_MM_DD = new SimpleDateFormat(
			"yyyy-MM-dd");
	/**
	 * 处理日期时，用到参数。格式为yyyyMMdd
	 */
	public static final SimpleDateFormat YYYYMMDD = new SimpleDateFormat(
			"yyyyMMdd");
	
	public static final SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT);
	

	/**
	 * 获得当前日期
	 * 
	 * @return
	 */
	public static String getCurrentDate(SimpleDateFormat sf) {
		if(sf==null){
			sf = YYYY_MM_DD;
		}
		return sf.format(new Date());
	}
	
	/**
	 * 获得某日期的指定格式的字符串
	 * @param sf
	 * @param date
	 * @return
	 */
	public static String getStingDate(SimpleDateFormat sf, Date date) {
		if(date==null){
			date = new Date();
		}
		if(sf==null){
			sf = YYYY_MM_DD;
		}
		
		return sf.format(date);
	}
	
	public static String getStringByLongStr(String longTime){
		return format.format(new Date(Long.parseLong(longTime)));
	}
	
	public static String getStringByLong(Long longTime){
		return format.format(new Date(longTime));
	}
	
	public static long getDaysFromBeinToEnd(SimpleDateFormat dateFormat,
			String begin, String end) {
		Date beginD = null;
		Date endD = null;
		long days = 0;
		try {
			if (begin != null && !"".equals(begin)) {
				beginD = dateFormat.parse(begin);
			} else {
				beginD = new Date();
			}
			if (end != null && !"".equals(end)) {
				endD = dateFormat.parse(end);
			} else {
				endD = new Date();
			}
			days = (endD.getTime() - beginD.getTime()) / (24 * 60 * 60 * 1000);
		} catch (ParseException e) {
			logger.error(ExceptionPrintUtils.printExceptionStack(e));
			return days;
		}

		return days;

	}
	public static long getMinutesFromBeinToEnd(long begin, long end) {
		return (end - begin) / (60 * 1000);
	}
	public static long getSecondsFromBeinToEnd(long begin, long end) {
		return (end - begin) / 1000;
	}
	
	/**
	 * 
	 * @param begin YYYY-MM-DD HH:mm:ss
	 * @param end YYYY-MM-DD HH:mm:ss
	 * @return
	 */
	public static long getMinutesFromBeinToEndStr(String begin, String end) {
		return (getLongDate(end,1) - getLongDate(begin,1)) / (60 * 1000);
	}
	
	/**
	 * 将字符串格式日期转换成long型
	 * @param strDate 格式：YYYY-MM-DD
	 * @return
	 */
	public static Long getLongDate(String strDate){
		Date date  = java.sql.Date.valueOf(strDate);
		Long lDate = new Long(date.getTime());
		return (lDate);
	}
	
	/**
	 * 将字符串格式日期转换成long型
	 * @param strDate 要转换的日期
	 * @param iType value strDate type
	 * 			0			:YYYY-MM-DD
	 * 			1			:YYYY-MM-DD HH:mm:ss
	 * @return
	 */
	public static Long getLongDate(String strDate,int iType){
		Long retDate = null;
		switch(iType){
		case 0 :
			retDate = getLongDate(strDate);
			break;
		case 1 :
			retDate = new Long(java.sql.Timestamp.valueOf(strDate).getTime());
		
		}
		return retDate;
		
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式的当前日期
	 * @return
	 */
	public static String getCurrentTime(){
		String s = format.format(new Date());
		return s;
	}
	
	public static Long toLongTime(String time) {
		Long longTime = null;
		try
		{
			if(StringUtils.isNotBlank(time)) {
				Date date = YYYY_MM_DD_HH24_MM_SS.parse(time);
				longTime = date.getTime();
				
			}
		} catch (Exception e)
		{
			logger.error("Date"+time+"is converted to long type exception", e);
		}
		return longTime;
	}
	
	/**
	 * 获取YYYY-MM-DD格式的当前日期
	 * @return
	 */
//	public static String getCurrentDate(){
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//		String s = df.format(new Date());
//		return s;
//	}
	
	/**
	 * 校验是否为时间类型
	 * @param str_input
	 * @param rDateFormat
	 * @return
	 */
	public static boolean isDate(String str_input,String rDateFormat){
		if (StringUtils.isNotBlank(str_input)) {
	         SimpleDateFormat formatter = new SimpleDateFormat(rDateFormat);
	         formatter.setLenient(false);
	         try {
	             formatter.format(formatter.parse(str_input));
	             return true;
	         } catch (Exception e) {
	             return false;
	         }
	     }
		return false;
	}
/*	public static void main(String args[]){
		System.out.println(DateUtil.getCurrentTime());
		System.out.println(getMinutesFromBeinToEndStr(DateUtil.getCurrentTime(),"2011-09-16 10:00:05"));
	}*/
	
}
