package com.dxhy.portrait.business.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author dxhy
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
public class DateUtils {
    public static java.util.Date getSqlDate() {
        java.util.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        return sqlDate;
    }

    public static java.util.Date getSqlDateLong() {
        java.util.Date currentTime = new java.util.Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(0);
        java.util.Date currentTime2 = formatter.parse(dateString, pos);
        return currentTime2;
    }

    public static java.util.Date getNowDate() {
        java.util.Date currentTime = new java.util.Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        java.util.Date currentTime2 = formatter.parse(dateString, pos);
        return currentTime2;
    }

    public static java.util.Date getNowDateShort() {
        java.util.Date currentTime = new java.util.Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        java.util.Date currentTime2 = formatter.parse(dateString, pos);
        return currentTime2;
    }

    public static String getStringDate() {
        java.util.Date currentTime = new java.util.Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getStringDateShort() {
        java.util.Date currentTime = new java.util.Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getTimeShort() {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        java.util.Date currentTime = new java.util.Date();
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static java.util.Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        java.util.Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static String dateToStrLong(java.util.Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static String dateToStr(java.util.Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static String dateToMonthStr(java.util.Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static java.util.Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        java.util.Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static java.util.Date strToDateChina(String strDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy��MM��dd��");
        ParsePosition pos = new ParsePosition(0);
        java.util.Date strtodate = sdf.parse(strDate, pos);
        return strtodate;
    }

    public static java.util.Date getNow() {
        java.util.Date currentTime = new java.util.Date();
        return currentTime;
    }

    public static java.util.Date getLastDate(long day) {
        java.util.Date date = new java.util.Date();
        long date3Hm = date.getTime() - 122400000L * day;
        java.util.Date date3HmDate = new java.util.Date(date3Hm);
        return date3HmDate;
    }

    public static String getStringToday() {
        java.util.Date currentTime = new java.util.Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getHour() {
        java.util.Date currentTime = new java.util.Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);

        String hour = dateString.substring(11, 13);
        return hour;
    }

    public static String getTime() {
        java.util.Date currentTime = new java.util.Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);

        String min = dateString.substring(14, 16);
        return min;
    }

    public static String getUserDate(String sformat) {
        java.util.Date currentTime = new java.util.Date();
        SimpleDateFormat formatter = new SimpleDateFormat(sformat);
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getTwoHour(String st1, String st2) {
        String[] kk = null;
        String[] jj = null;
        kk = st1.split(":");
        jj = st2.split(":");
        if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0])) {
            return "0";
        }
        double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60.0D;

        double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60.0D;
        if (y - u > 0.0D) {
            return y - u + "";
        }
        return "0";
    }

    public static String getTwoDay(String sj1, String sj2) {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        long day = 0L;
        try {
            java.util.Date date = myFormatter.parse(sj1);
            java.util.Date mydate = myFormatter.parse(sj2);
            day = (date.getTime() - mydate.getTime()) / 86400000L;
        } catch (Exception e) {
            return "";
        }
        return day + "";
    }

    public static String getPreTime(String sj1, String jj) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String mydate1 = "";
        try {
            java.util.Date date1 = format.parse(sj1);
            long time = date1.getTime() / 1000L + Integer.parseInt(jj) * 60;
            date1.setTime(time * 1000L);
            mydate1 = format.format(date1);
        } catch (Exception localException) {
        }
        return mydate1;
    }

    public static String getNextDay(String nowdate, String delay) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String mdate = "";
            java.util.Date d = strToDate(nowdate);
            long myTime = d.getTime() / 1000L + Integer.parseInt(delay) * 24 * 60 * 60;

            d.setTime(myTime * 1000L);
            return format.format(d);
        } catch (Exception e) {
        }
        return "";
    }

    public static boolean isLeapYear(String ddate) {
        java.util.Date d = strToDate(ddate);
        GregorianCalendar gc = (GregorianCalendar)Calendar.getInstance();
        gc.setTime(d);
        int year = gc.get(Calendar.YEAR);
        if (year % 400 == 0) {
            return true;
        }
        if (year % 4 == 0) {
            return year % 100 != 0;
        }
        return false;
    }

    public static boolean rightDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        if (date == null) {
            return false;
        }
        if (date.length() > 10) {
            sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        } else {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        }
        try {
            sdf.parse(date);
        } catch (ParseException pe) {
            return false;
        }
        return true;
    }

    public static String getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {
        java.util.Date currentTime = new java.util.Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        String sNd = dateString.substring(0, 4);
        String sYf = dateString.substring(5, 7);
        String sRq = dateString.substring(8, 10);
        String sreturn = "";
        if ((sdate == null) || ("".equals(sdate)) || (!rightDate(sdate))) {
            if ("1".equals(nd)) {
                sreturn = sNd;
                if ("1".equals(format)) {
                    sreturn = sreturn + "��";
                } else if ("2".equals(format)) {
                    sreturn = sreturn + "-";
                } else if ("3".equals(format)) {
                    sreturn = sreturn + "/";
                } else if ("5".equals(format)) {
                    sreturn = sreturn + ".";
                }
            }
            if ("1".equals(yf)) {
                sreturn = sreturn + sYf;
                if ("1".equals(format)) {
                    sreturn = sreturn + "��";
                } else if ("2".equals(format)) {
                    sreturn = sreturn + "-";
                } else if ("3".equals(format)) {
                    sreturn = sreturn + "/";
                } else if ("5".equals(format)) {
                    sreturn = sreturn + ".";
                }
            }
            if ("1".equals(rq)) {
                sreturn = sreturn + sRq;
                if ("1".equals(format)) {
                    sreturn = sreturn + "��";
                }
            }
        } else {
            sNd = sdate.substring(0, 4);
            sYf = sdate.substring(5, 7);
            sRq = sdate.substring(8, 10);
            if ("1".equals(nd)) {
                sreturn = sNd;
                if ("1".equals(format)) {
                    sreturn = sreturn + "��";
                } else if ("2".equals(format)) {
                    sreturn = sreturn + "-";
                } else if ("3".equals(format)) {
                    sreturn = sreturn + "/";
                } else if ("5".equals(format)) {
                    sreturn = sreturn + ".";
                }
            }
            if ("1".equals(yf)) {
                sreturn = sreturn + sYf;
                if ("1".equals(format)) {
                    sreturn = sreturn + "��";
                } else if ("2".equals(format)) {
                    sreturn = sreturn + "-";
                } else if ("3".equals(format)) {
                    sreturn = sreturn + "/";
                } else if ("5".equals(format)) {
                    sreturn = sreturn + ".";
                }
            }
            if ("1".equals(rq)) {
                sreturn = sreturn + sRq;
                if ("1".equals(format)) {
                    sreturn = sreturn + "��";
                }
            }
        }
        return sreturn;
    }

    public static String getNextMonthDay(String sdate, int m) {
        sdate = getOkDate(sdate);
        int year = Integer.parseInt(sdate.substring(0, 4));
        int month = Integer.parseInt(sdate.substring(5, 7));
        month += m;
        if (month < 0) {
            month += 12;
            year -= 1;
        } else if (month > 12) {
            month -= 12;
            year += 1;
        }
        String smonth = "";
        if (month < 10) {
            smonth = "0" + month;
        } else {
            smonth = "" + month;
        }
        return year + "-" + smonth + "-10";
    }

    public static String getOkDate(String sdate) {
        if ((sdate == null) || ("".equals(sdate))) {
            return getStringDateShort();
        }
        if (!rightDate(sdate)) {
            sdate = getStringDateShort();
        }
        sdate = sdate.replace("/", "-");
        if (sdate.length() == 8) {
            sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-" + sdate.substring(6, 8);
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        java.util.Date strtodate = formatter.parse(sdate, pos);
        String dateString = formatter.format(strtodate);
        return dateString;
    }

    public static java.util.Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1, year);
        java.util.Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    public static java.util.Date getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1, year);
        calendar.roll(6, -1);
        java.util.Date currYearLast = calendar.getTime();

        return currYearLast;
    }

    public static String getNowYyyyMmDd() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        String dateString = formatter.format(new java.util.Date());
        return dateString;
    }

    public static java.util.Date getMorning(java.util.Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(10, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        return calendar.getTime();
    }

    public static java.util.Date getNight(java.util.Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(10, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        return calendar.getTime();
    }

    public static java.util.Date strToDatePeriod(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        ParsePosition pos = new ParsePosition(0);
        java.util.Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static String strToStrDate(String strDate) {
        String strtostr = "";
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMdd");
            java.util.Date date = formatter2.parse(strDate);
            strtostr = formatter.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strtostr;
    }

    public static String strToStrDate2(String strDate) {
        String strtostr = "";
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMdd");
            java.util.Date date = formatter.parse(strDate);
            strtostr = formatter2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strtostr;
    }

    /**
     * 将时间戳转换为时间
     *
     * @param lt
     * @return
     */
    public static Date stampToDate(int lt) {
        Date res = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(lt);
        try {
            res = simpleDateFormat.parse(simpleDateFormat.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static void main(String[] args) throws Exception {}
}
