package com.iwunu.nbiot.common.util;


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

public class DateUtil {
	public final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//10
	public final static SimpleDateFormat dateFormatYYYYMM = new SimpleDateFormat("yyyy-MM");//7
	public final static SimpleDateFormat dateFormat8 = new SimpleDateFormat("yyyyMMdd");//8
	public final static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//19
	public final static SimpleDateFormat dateFormatYYYYMMDDHH = new SimpleDateFormat("yyyy-MM-dd HH");//13
	public final static SimpleDateFormat dateFormatYYYYMMDDHHmm = new SimpleDateFormat("yyyy-MM-dd HH:mm");//16
	public final static SimpleDateFormat dateFormatYYYYMMDDHHmmSSS = new SimpleDateFormat("yyMMddHHmmss");//23
	
	public final static SimpleDateFormat[] dateFormats = new SimpleDateFormat[]{null,
			null,null,null,null,null,null,dateFormatYYYYMM,dateFormat8,null,dateFormat,null,null,dateFormatYYYYMMDDHH
			,null,null,dateFormatYYYYMMDDHHmm, null, null, dateTimeFormat, null, null, null, dateFormatYYYYMMDDHHmmSSS
	};
	
	public static <T> Object to(String dateStr, Class<T> toDateClass){
		if(toDateClass==Date.class){
			return toDate(dateStr);
		}else if(toDateClass==Timestamp.class){
			return toTimestamp(dateStr);
		}else if(toDateClass==java.sql.Date.class){
			return toSqlDate(dateStr);
		}
		return null;
	}
	
	public static Date toDate(String dateStr){
		if(dateStr==null){
			return null;
		}
		return toDate(dateStr, dateFormats[dateStr.length()]);
	}
	
	public static Date toDate(String dateStr, String formatStr){
		return toDate(dateStr, new SimpleDateFormat(formatStr));
	}
	
	public static Date toDate(String dateStr, SimpleDateFormat format){
		if(dateStr==null || "".equals(dateStr)){
			return null;
		}
		if(format==null){
			return null;
		}
		try {
			return format.parse(dateStr);
		} catch (ParseException e) {
			throw new RuntimeException("解析"+dateStr+"成日期异常");
		}
	}
	
	public static Timestamp toTimestamp(String dateStr){
		if(dateStr==null){
			return null;
		}
		return toTimestamp(dateStr, dateFormats[dateStr.length()]);
	}
	
	public static Timestamp toTimestamp(String dateStr, String formatStr){
		return toTimestamp(dateStr, formatStr);
	}
	
	public static Timestamp toTimestamp(String dateStr, SimpleDateFormat format){
		Date date = toDate(dateStr, format);
		if(date==null){
			return null;
		}
		return new Timestamp(date.getTime());
	}
	
	public static java.sql.Date toSqlDate(String dateStr){
		if(dateStr==null){
			return null;
		}
		return toSqlDate(dateStr, dateFormats[dateStr.length()]);
	}
	
	public static java.sql.Date toSqlDate(String dateStr, String formatStr){
		return toSqlDate(dateStr, formatStr);
	}
	
	public static java.sql.Date toSqlDate(String dateStr, SimpleDateFormat format){
		Date date = toDate(dateStr, format);
		if(date==null){
			return null;
		}
		return new java.sql.Date(date.getTime());
	}
	
	public static String nowDate(){
		return dateFormat.format(System.currentTimeMillis());
	}
	public static String now(String pattern){
		return new SimpleDateFormat(pattern).format(System.currentTimeMillis());
	}
	public static String now(SimpleDateFormat simpleDateFormat){
		return simpleDateFormat.format(System.currentTimeMillis());
	}
	public static String nowDateTime(){
		return dateTimeFormat.format(System.currentTimeMillis());
	}


	public static Integer getNowTime(){
		Long time = System.currentTimeMillis()/1000L ;
		return time.intValue() ;
	}



	public static int getSecondTimestampTwo(Date date){
		if (null == date) {
			return 0;
		}
		String timestamp = String.valueOf(date.getTime()/1000);
		return Integer.valueOf(timestamp);
	}
	public static String stampToDate(long lt){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    return simpleDateFormat.format(new Date(Long.valueOf(lt+"000"))); 
	}
	public static String dateToStamp(String s) throws ParseException{
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = simpleDateFormat.parse(s);
		long ts = date.getTime();
		res = String.valueOf(ts);
		return res;
	}
	/**
	 * 转换下发的时间16进制数据
	 * @param s
	 * @return
	 */
	public static String dateToStampHex(String s) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long ts = date.getTime() / 1000 + 60 * 60 * 8;
		return HexUtils.toHexL(ts+"", 8);
	}
	/**
	 * 获取当前时间戳
	 * @return
	 */
	public static String getStamp() {
		return HexUtils.toHexL(DateUtil.getNowTime()+ 60 * 60 * 8 + "", 8);
	}

	public static void main(String[] args) throws ParseException {
		System.out.println(nowDateTime()) ;
		System.out.println(stampToDate(1547140725));
		System.out.println(timeStamp2Date("1547140725","yyyy-MM-dd HH:mm:ss"));
	}

	public static String timeStamp2Date(String seconds,String format) {
		if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
			return "";
		}
		if(format == null || format.isEmpty()){
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(Long.valueOf(seconds+"000")));
	  }
	

}
