package com.sjkj.base_lib.camera.dh;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class TimeUtils {

	public static String getDateCN() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
		String date = format.format(new Date(System.currentTimeMillis()));
		return date;// 2012年10月03日 23:41:31
	}

	public static String getDateEN() {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date1 = format1.format(new Date(System.currentTimeMillis()));
		return date1;// 2012-10-03 23:41:31
	}

	public static String getDateYMD(long time, String pattern) {
		SimpleDateFormat format1 = new SimpleDateFormat(pattern);
		Date date = new Date(time);
		String date1 = format1.format(date);
		return date1;// 2012-10-03 23:41:31
	}

	public static String getDateYMD(long time) {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date(time);
		String date1 = format1.format(date);
		return date1;// 2012-10-03 23:41:31
	}

	public static String getDateEN(long time) {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date date = new Date(time);
		String date1 = format1.format(date);
		return date1;// 2012-10-03 23:41:31
	}

	public static String getDateEN(long time, String id) {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		format1.setTimeZone(TimeZone.getTimeZone(id));
		Date date = new Date(time);
		String date1 = format1.format(date);
		return date1;// 2012-10-03 23:41:31
	}

	public static String getDateZoneEN(long time) {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
		Date date = new Date(time);
		String date1 = format1.format(date);
		return date1.substring(0, date1.length() - 2) + ":" + date1.substring(date1.length() - 2);
	}

	public static String getDateHMS(long time) {
		SimpleDateFormat format1 = new SimpleDateFormat("HH:mm:ss");
		Date date = new Date(time);
		String date1 = format1.format(date);
		return date1;// 2012-10-03 23:41:31
	}

	public static String getDateHMSGMT(long time) {
		SimpleDateFormat format1 = new SimpleDateFormat("HH:mm:ss");
		format1.setTimeZone(TimeZone.getTimeZone("GMT"));
		Date date = new Date(time);
		String date1 = format1.format(date);
		return date1;// 2012-10-03 23:41:31
	}

	public static String getTime(long time) {
		SimpleDateFormat format1 = new SimpleDateFormat("HH:mm:ss");
		Date date = new Date(time);
		String date1 = format1.format(date);
		return date1;// 23:41:31
	}

	public static String getTime4HM(long time) {
		SimpleDateFormat format1 = new SimpleDateFormat("HH:mm");
		Date date = new Date(time);
		String date1 = format1.format(date);
		return date1;// 23:41:31
	}

	public static String getTimeHM(long time) {
		int hour = (int) (time / 60 / 60);
		int min = (int) (time / 60 % 60);
		String result = "";
		if (hour == 0) {
			result = "00:";
		} else if (hour < 10) {
			result = "0" + hour + ":";
		} else if (hour < 24) {
			result = hour + ":";
		}
		if (min == 0) {
			result += "00";
		} else if (min < 10) {
			result += "0" + min + "";
		} else if (min < 60) {
			result += min;
		}

		return result + ":00";// 23:41:00
	}

	public static String getTimeHMS(long time) {
		int hour = (int) (time / 60 / 60);
		int min = (int) (time / 60 % 60);
		int second = (int) (time - hour * 3600 - min * 60);
		String result = "";
		if (hour == 0) {
			result = "00:";
		} else if (hour < 10) {
			result = "0" + hour + ":";
		} else {
			result = hour + ":";
		}
		if (min == 0) {
			result += "00:";
		} else if (min < 10) {
			result += "0" + min + ":";
		} else if (min < 60) {
			result += min + ":";
		}
		if (second == 0) {
			result += "00";
		} else if (second < 10) {
			result += "0" + second;
		} else if (second < 60) {
			result += second;
		}
		return result;// 23:41:00
	}

	public static String getDate() {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		String date = format.format(new Date(System.currentTimeMillis()));
		return date;
	}

	public static long getTimeStamp(String time) {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date date = null;
		try {
			date = format1.parse(time);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (date == null) {
			return -1;
		}

		return date.getTime();
	}
	public static long getTimeStampZoneEN(String time) {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

		Date date = null;
		try {
			date = format1.parse(time);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (date == null) {
			return -1;
		}

		return date.getTime();
	}
	public static long getTimeStampHMS(String time) {
		SimpleDateFormat format1 = new SimpleDateFormat("HH:mm:ss");

		Date date = null;
		try {
			date = format1.parse(time);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (date == null) {
			return -1;
		}

		return date.getTime();
	}

	public static long getTimeStampHM(String time) {
		String ss[] = time.split(":");
		if (ss.length != 3)
			return 0;
		else {
			int h = Integer.parseInt(ss[0]);
			long i = (Integer.parseInt(ss[0]) * 60L + Integer.parseInt(ss[1])) * 60L;
			return i;
		}
	}

	// 获取某一天的开始时间
	public static long getStartTimeByDay(Calendar c) {
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTimeInMillis();
	}

	// 获取某一天的结束时间
	public static long getEndTimeByDay(Calendar c) {
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		return c.getTimeInMillis();
	}

	// 毫秒转小时
	public static int getHourByMillSecond(long millSecond) {
		int hour = (int) (millSecond / (1000 * 3600));
		return hour;
	}

	// 毫秒转换成除去小时后剩余分钟数
	public static int getMinuteByMillSecond(long millSecond) {
		int minute = (int) ((millSecond - getHourByMillSecond(millSecond) * 3600 * 1000) / (1000 * 60));
		return minute;
	}

	public static int getHourBySecond(long second) {
		return (int) (second / 60 / 60);
	}

	public static int getMinuteBySecond(long second) {
		return (int) (second / 60 % 60);
	}

	/**
	 * 获取UTC时间
	 * @param time
	 * @return
	 */
	public static String getUTCTimeStr(String time) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
		StringBuffer UTCTimeBuffer = new StringBuffer();
		Date date;
		// 1、取得本地时间：
		Calendar cal = Calendar.getInstance() ;
		try {
			date = format.parse(time);
			cal.setTime(date);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		// 2、取得时间偏移量：
		int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
		// 3、取得夏令时差：
		int dstOffset = cal.get(Calendar.DST_OFFSET);
		// 4、从本地时间里扣除这些差量，即可以取得UTC时间：
		cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);
		UTCTimeBuffer.append(year).append("-").append(month < 10 ? "0" + month : month).append("-").append(day  < 10 ? "0" + day : day) ;
		UTCTimeBuffer.append(" ").append(hour < 10 ? "0" + hour : hour).append(":").append(minute < 10 ? "0" + minute : minute).append(":").append(second < 10 ? "0" + second : second) ;
		try{
			format.parse(UTCTimeBuffer.toString()) ;
			return UTCTimeBuffer.toString() ;
		}catch(ParseException e)
		{
			e.printStackTrace() ;
		}
		return null ;
	}



	/**
	 * 函数功能描述:UTC时间转本地时间格式
	 * @param utcTime UTC时间
	 * @param utcTimePatten UTC时间格式
	 * @param localTimePatten   本地时间格式
	 * @return 本地时间格式的时间
	 * eg:utc2Local("2017-06-14 09:37:50.788+08:00", "yyyy-MM-dd HH:mm:ss.SSSXXX", "yyyy-MM-dd HH:mm:ss.SSS")
	 */
	public static String utc2Local(String utcTime, String utcTimePatten, String localTimePatten) {
		SimpleDateFormat utcFormater = new SimpleDateFormat(utcTimePatten);
		utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));//时区定义并进行时间获取
		Date gpsUTCDate = null;
		try {
			gpsUTCDate = utcFormater.parse(utcTime);
		} catch (ParseException e) {
			e.printStackTrace();
			return utcTime;
		}
		SimpleDateFormat localFormater = new SimpleDateFormat(localTimePatten);
		localFormater.setTimeZone(TimeZone.getDefault());
		String localTime = localFormater.format(gpsUTCDate.getTime());
		return localTime;
	}


	public static long utcToLong(String utcTime, String utcTimePatten) {
		long time = 0;
		try {
			SimpleDateFormat utcFormater = new SimpleDateFormat(utcTimePatten);
			utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
			Date date = utcFormater.parse(utcTime);

			time = date.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 判断输入时间所处位置
	 * @param inputTime 格式为yyyy-mm-dd HH:mm:ss
	 * @return
	 */
	public static String timePosition(String inputTime){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
		try{
			long millionTime = format.parse(inputTime).getTime();
			Date date = new Date();
			long currentTime = date.getTime();
			long abs = Math.abs(millionTime-currentTime);
			int index = (int) (abs/1000/3600);
			Date date2 = format.parse(inputTime);
			Calendar cal1 = Calendar.getInstance() ;
			cal1.setTime(date2);
			if (index<=24){
				int hour = cal1.get(Calendar.HOUR_OF_DAY);
				int minute = cal1.get(Calendar.MINUTE);
				return hour+":"+minute;
			}else if(index>24&&index<48){
				return "昨天";
			}else {
				int year = cal1.get(Calendar.YEAR);
				int month = cal1.get(Calendar.MONTH)+1;
				int day = cal1.get(Calendar.DAY_OF_MONTH);
				String yearSub = year+"";
				String sub = yearSub.substring(2,4);
				return sub+"/"+month+"/"+day;
			}
		} catch (Exception e){
			e.toString();
		}
		return "";
	}

	public static String getYear(){
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy");
		String year = format1.format(new Date(System.currentTimeMillis()));
		return year;// 2012
	}

	public static String timeToDate(long time,String format){
		SimpleDateFormat dateformat = new SimpleDateFormat(format);
		String formatDate = dateformat.format(new Date(time));
		return formatDate;
	}

	public static long dateToTime(String date,String format){
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		long time = 0;
		try {
			time =  dateFormat.parse(date).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return time;
	}
	/**
	 * 将字符串时间格式转换成Date时间格式，参数String类型
	 * 比如字符串时间："2017-12-15 21:49:03"
	 * 转换后的date时间：Fri Dec 15 21:49:03 CST 2017
	 * @param datetime 类型为String
	 * @return
	 */
	public static Date StringToDate(String datetime){
		SimpleDateFormat sdFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		try {
			date = sdFormat.parse(datetime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	public static boolean compareStringTime(String startTime,String endTime){
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dateStart = simpleDateFormat.parse(startTime);
			Date dateEnd = simpleDateFormat.parse(endTime);
			if (dateEnd.compareTo(dateStart)==0){//相等
				return true;
			}else if (dateEnd.compareTo(dateStart)>=1){//dateEnd 大
				return true;
			}else {
				return false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * 时间格式化工具
	 *
	 * @param pattern    格式
	 * @param timeMillis 毫秒
	 */
	private static SimpleDateFormat sDateYMDFormat;

	public static String dataFormat(String pattern, long timeMillis) {//这个传多了一个参数,pattern模型,就是自己规定格式
		if (sDateYMDFormat == null) {
			sDateYMDFormat = new SimpleDateFormat(pattern, Locale.getDefault());//跟上面差不多的
		}
		try {
			return sDateYMDFormat.format(timeMillis);//跟上面差不多的
		} catch (Exception e) {
			return "2022-01-01";//跟上面差不多的
		}
	}
}
