package com.base.orbit.utils.tool;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateTool {
	public static XMLGregorianCalendar getXMLTime() {		
		String format = "yyyy-MM-dd'T'hh:mm:ss'Z'";
		SimpleDateFormat formater = new SimpleDateFormat(format);
		String s = formater.format(new Date());
		XMLGregorianCalendar date = null;
		try {
			date = DatatypeFactory.newInstance().newXMLGregorianCalendar(s);
		} catch (DatatypeConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;

	}

	public static String getTime1() {
		Date d = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = df.format(d);
		return s;
	}

	public static String getTime2() {
		Date d = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String s = df.format(d);
		return s;
	}

	public static String getTime3() {
		Date d = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
		String s = df.format(d);
		return s;
	}

	public static String getTime4() {
		Date d = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String s = df.format(d);
		return s;
	}
	public static String getTime5() {
		Date d = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
		String s = df.format(d);
		return s;
	}
	
	public static String getTime6() {
		Date d = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		String s = df.format(d);
		return s;
	}

	//当前时间+space得到的时间   注意格式 space的单位是小时
	public static String getLaterTime1(int space) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date d = new Date();
		long t = (d.getTime() / 1000) + space * 60 * 60;
		d.setTime(t * 1000);
		String laterTime = df.format(d);

		return laterTime;
	}
	
	//当前时间+space得到的时间   注意格式 space的单位是秒
	public static String getLaterSecond(int space) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date d = new Date();
		long t = (d.getTime() / 1000) + space ;
		d.setTime(t * 1000);
		String laterTime = df.format(d);

		return laterTime;
	}

	public static String getLaterTimeByHour(Date d,int space) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long t = (d.getTime() / 1000) + space * 60 * 60;
		d.setTime(t * 1000);
		String laterTime = df.format(d);
		return laterTime;
	}
	//2019-6-28 新增by qinpch
	public static Date getLaterTimeBySecond(Date d,int space) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long t = (d.getTime() / 1000) + space;
		//Calendar calendar = Calendar.getInstance();
		//d.setTime(t * 1000);
		Date date = new Date(t * 1000);
		String laterTime = df.format(date);
		//System.out.println(laterTime);
		return date;
	}
	
	public static String getLaterTime3(int space) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy MM dd HH:mm:ss");

		Date d = new Date();
		long t = (d.getTime() / 1000) + space * 60 * 60;
		d.setTime(t * 1000);
		String laterTime = df.format(d);

		return laterTime;
	}

	public static String getDate() {
		Date d = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		String s = df.format(d);
		return s;
	}
	public static Date StringToDate(String s) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date d = df.parse(s);
		return d;
	}
	public static String DateToString(Date d)  {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String s = df.format(d);		
		return s;
		
	}
	// huzhj 20171122
	public static String DateToString1(Date d)  {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		if(d == null){
			d = new Date();
		}
		String s = df.format(d);		
		return s;
		
	}
	
	public static String DateToString2(Date d)  {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = df.format(d);		
		return s;
		
	}
	
	public static String getDateYYMMDD() {
		Date d = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyMMdd");
		String s = df.format(d);
		return s;
	}

	//
	public static long getDuration(String t1, String t2){
		Date date1 = StringToDate1(t1);
		Date date2 = StringToDate1(t2);
		long result = date1.getTime()-date2.getTime();
		return result;
	}
	public static String getMonth() {
		Date d = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyMM");
		String s = df.format(d);
		return s;
	}

	public static Date StringToDate1(String s) {
		try {
			SimpleDateFormat df = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");			
			Date d = df.parse(s);
		
			return d;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date StringToDate3(String s) {
		try {
			SimpleDateFormat df = new SimpleDateFormat(
					"yyyy MM dd HH:mm:ss");
			Date d = df.parse(s);

			return d;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Date StringToDate4(String s) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
			Date d = df.parse(s);
			return d;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

    ////////////////////////下面是比较大小的方法////////////////////////
    
    /**
     * 方法说明 : 比较date1和date2的大小
     * @param date1
     * @param date2
     * @return：0表示date1=date2;1表示date1>date2;-1表示date1<date2
     */
    public static int DateCompare(Date date1, Date date2){
    	long result = date1.getTime()-date2.getTime();
    	
    	if(result>0){
    		return 1;
    	}else if(result==0){
    		return 0;
    	}else{
    		return -1;
    	}
    }
    /**
     * 方法说明 : 比较time和当前时间的大小
     * @param time
     * @param format : 格式中由yyyy MM dd HH mm ss组合形成，例如format=“yyyy-MM-dd HH:mm:ss”
     * @return : 0表示date=当前时间;1表示date>当前时间;-1表示date<当前时间
     */
    public static int DateCompare(String time, String format){

    	Date date = getTimebyType(format, time);
    	return DateCompare(date, new Date());
    }
    /**
	 * 方法说明 : 将字符转换为时间
	 * @param format : 格式中由yyyy MM dd HH mm ss组合形成，例如format=“yyyy-MM-dd HH:mm:ss”
	 * @param time : 指定时间
	 * @return :  时间
	 */
	public static Date getTimebyType(String formatString, String timeString){
		String year = timeString.substring(formatString.indexOf("yyyy"),formatString.indexOf("yyyy")+4);
		String month = timeString.substring(formatString.indexOf("MM"),formatString.indexOf("MM")+2);
		String day = timeString.substring(formatString.indexOf("dd"),formatString.indexOf("dd")+2);
		String hour = timeString.substring(formatString.indexOf("HH"),formatString.indexOf("HH")+2);
		String minitue = timeString.substring(formatString.indexOf("mm"),formatString.indexOf("mm")+2);
		String second = timeString.substring(formatString.indexOf("ss"),formatString.indexOf("ss")+2);
		
		if(year==null || month==null || day==null || hour==null || minitue==null || second==null){
			return null;
		}
		
		String timenew = year+month+day+hour+minitue+second;
		
		try{
			SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
			Date d=df.parse(timenew);

			return d;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	//两个任务时间段冲突检测
	public static Boolean DateCompare1(String start1, String end1,
			String start2, String end2) {
		Date s1 = DateTool.StringToDate1(start1);
		Date e1 = DateTool.StringToDate1(end1);
		Date s2 = DateTool.StringToDate1(start2);
		Date e2 = DateTool.StringToDate1(end2);

		if (s1.after(s2) == true && s1.after(e2) == true) {
			return false;
		}
		if (e1.after(s2) == false && e1.after(e2) == false) {
			return false;
		}
		return true;
	}

	public static Boolean DateCompare3(String start1, String end1,
			String start2, String end2) {
		Date s1 = DateTool.StringToDate3(start1);
		Date e1 = DateTool.StringToDate3(end1);
		Date s2 = DateTool.StringToDate3(start2);
		Date e2 = DateTool.StringToDate3(end2);

		if (s1.after(s2) == true && s1.after(e2) == true) {
			return false;
		}
		if (e1.after(s2) == false && e1.after(e2) == false) {
			return false;
		}
		return true;
	}

	//任务开始时间和结束时间冲突检测
	public static Boolean DateCompare1(String start, String end) {
		Date s = DateTool.StringToDate1(start);
		Date e = DateTool.StringToDate1(end);

		return s.after(e);
	}

	public static Boolean DateCompare3(String start, String end) {
		Date s = DateTool.StringToDate3(start);
		Date e = DateTool.StringToDate3(end);

		return s.after(e);
	}

	//任务结束时间与系统时间的冲突检测
	public static Boolean DateComparetoSystem1(String end) {
		Date e = DateTool.StringToDate1(end);
		Date sys = DateTool.StringToDate1(DateTool.getTime6());
		return sys.after(e);
	}

	public static Boolean DateComparetoSystem3(String end) {
		Date e = DateTool.StringToDate3(end);
		Date sys = DateTool.StringToDate3(DateTool.getTime3());
		return sys.after(e);
	}

	//发任务时的一个检测
	public static Boolean DateCompareSpace1(String start) {
		Date e = DateTool.StringToDate1(start);
		Date sys = DateTool.StringToDate1(DateTool.getTime6());
		long s = (e.getTime() - sys.getTime()) / 1000;
		if (s <= 0) {
			return false;
		} else {
			return true;
		}
	}

	public static Boolean DateCompareSpace3(String start) {
		Date e = DateTool.StringToDate3(start);
		Date sys = DateTool.StringToDate3(DateTool.getTime3());
		long s = (e.getTime() - sys.getTime()) / 1000;
		if (s <= 0) {
			return false;
		} else {
			return true;
		}
	}

	public static Boolean DateCompareSpace4(String start, int space) {
		Date e = DateTool.StringToDate3(start);
		Date sys = DateTool.StringToDate3(DateTool.getTime3());
		long s = e.getTime() - sys.getTime() - space * 1000;
		if (s <= 0) {
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * 将输入的时间转为世界时
	 * @param d 输入的时间参数
	 * @param hour 与界面时相差小时数，有正负号
	 * @return
	 */
	public static Date changeToUTC(Date d, int hour) {
		if(d==null){
			return null;
		}
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(d);
		calendar.add(Calendar.HOUR, hour);
		
		return (Date)calendar.getTime();
	}

	//
	//Date d
	// long delay:mico second
	public static String getDelayTime(Date d, long delay) {

		long l = d.getTime();
		l = l + delay;

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		String s = df.format(l);
		return s;
	}
	
	// 20170527 add by chenhy
	public static String getDelayDay(Date d, int delay) {
		
		Calendar calendar=new GregorianCalendar();
		calendar.setTime(d);
		calendar.add(Calendar.DATE, delay);
		d=calendar.getTime();
		long l = d.getTime();

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		String s = df.format(l);
		return s;
	}
	// 20190530 add by qinpch
		public static String getDelayOneDay(Date d, int delay) {
			
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(d);
			calendar.add(Calendar.DATE, delay);
			d=calendar.getTime();
			long l = d.getTime();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String s = df.format(l);
			//System.err.println(s + "---------------------");
			return s;
		}

	// 一个地址转换工具---与该类无关
	public static String dirProcess(String dir) {
		String s;
		s = dir.trim();
		s = dir.replace("/", "\\");
		s = s.replace("\\\\", "\\");
		if (s.substring(s.length() - 1).equals("\\")) {
			s = s.substring(0, s.length() - 1);
		}
		return s;
	}

	public static void main(String[] args) {
//		Date d = new Date();
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
//		String s = df.format(d);
		try {

			String s = "2014-04-02";
			Date d = StringToDate(s);
			System.out.println(getDelayTime(d, 10 * 1000));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//System.out.println(getDelayTime(d, 10 * 1000));
		

	}
}
