package com.qhclh.ytzh.utils;

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


public final class DateTimeUtil {
    static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    static SimpleDateFormat dateFormatDetail = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static Date ParseDate(String dateStr) throws ParseException {
        return dateFormat.parse(dateStr);
    }

    public static Date ParseDateDetail(String dateStr) throws ParseException {
        return dateFormatDetail.parse(dateStr);
    }

    public static Date SafeParseDate(String dateStr) {
        try {
            if (StringUtil.IsNullOrEmpty(dateStr)) {
                return new Date();
            }
            return ParseDate(dateStr);
        } catch (ParseException e) {
            return new Date();
        }
    }


    public static Date SafeParseDateDetail(String dateStr) {
        try {
            if (StringUtil.IsNullOrEmpty(dateStr)) {
                return new Date();
            }
            return ParseDateDetail(dateStr);
        } catch (ParseException e) {
            return new Date();
        }
    }

    public static Calendar SafeParseCalendar(String dateStr) {
        Calendar calendar = Calendar.getInstance(Locale.getDefault());
        calendar.setTime(SafeParseDate(dateStr));
        return calendar;
    }

    public static String FormatDate(Calendar calendar) {
        return dateFormat.format(calendar.getTime());
    }

    public static String FormatDate(Date date) {
        if (date == null) {
            return "";
        }
        return dateFormat.format(date);
    }

    public static String FormatDateDetail(Date date) {
        if (date == null) {
            return "";
        }
        return dateFormatDetail.format(date);
    }

    public static Date RemoveTime(Date time) {
        return SafeParseDate(FormatDate(time));
    }


    public static String getYMD() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    public static String getYMD(Date string) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String str = formatter.format(string);
        return str;
    }

    public static String getYMD2(Date string) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        String str = formatter.format(string);
        return str;
    }

    public static String getYMD3(Date string) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String str = formatter.format(string);
        return str;
    }

    public static String getYMDHMS() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    public static String getYMDHMS1() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    public static String getStrYMD(String str) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = null;
        try {
            date = df.parse(str.trim());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        String stra = formatter.format(date);
        return stra;
    }

    public static long getDays(Date d1, Date d2) {
        long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
        long days = diff / (1000 * 60 * 60 * 24);
        return days;
    }

    /*时间戳转换成字符窜*/
    public static String getDateToString(long time) {
        Date d = new Date(time*1000);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return sf.format(d);
    }

}
