package com.ibeetl.admin.core.util;

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


public class DateUtil {
    
    public static Date MAX_DATE = maxDate();
   
    
    
    public static String now() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }
    public static String nowtime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }
    public static String now(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }
    
    private static Date maxDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse("9999-12-31 23:59:59");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    public static String getStringYMDH(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static Date stringToDate(String str, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String date2String(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
    /**
	 * 计算两个参数时间相差多久
	 * 
	 * @param stardate
	 * @param enddate
	 * @return
	 * @throws Exception
	 */
	public static long mathday(String stardate, String enddate, String format)
			throws Exception {

		Date starttime = DateUtil.getDate(stardate, format);
		Date endtime = DateUtil.getDate(enddate, format);
		return (endtime.getTime() - starttime.getTime());
	}
	/**
	 * String转成次格式的date
	 * 
	 * @param strDate
	 * @return
	 * @throws Exception
	 */
	public static Date getDate(String strDate, String format) throws Exception {

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = sdf.parse(strDate);
		return date;
	}
	
	/**
	 * String转成次格式的String
	 * 
	 * @param strDate
	 * @return
	 * @throws Exception
	 */
	public static String getStringto2(String strDate, String format,String newformat)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = sdf.parse(strDate);
		
		String dateStr = new SimpleDateFormat(newformat).format(date);
		return dateStr;
	}

	public static String getTimeDifference(String time1, String time2) {
		SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long t1 = 0L;
		long t2 = 0L;
		try {
			t1 = timeformat.parse(time1).getTime();
		} catch (ParseException e) {
// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			t2 = timeformat.parse(time2).getTime();
		} catch (ParseException e) {
// TODO Auto-generated catch block
			e.printStackTrace();
		}
//因为t1-t2得到的是毫秒级,所以要初3600000得出小时.算天数或秒同理
		int hours=(int) ((t1 - t2)/3600000);
		int minutes=(int) (((t1 - t2)/1000-hours*3600)/60);
		int second=(int) ((t1 - t2)/1000-hours*3600-minutes*60);
//		return ""+hours+"小时"+minutes+"分"+second+"秒";
		return String.valueOf(minutes);
	}

	public static void main(String[] args) throws Exception {
		System.out.println(getTimeDifference("2021-12-14 20:44:22","2021-12-14 20:41:12"));
		System.out.println(mathday("2021-09-11 10:41:12",DateUtil.now("yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd HH:mm:ss")/(3600000*24));
		System.out.println("date2String = " + date2String(new Date(),"yyyyMMdd"));
		System.out.println("date2String = " + date2String(new Date(),"YYYYmmdd"));
	}

}
