package com.xty.common;


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

public class Dateutils {

    public static final String HH_MM_Format = "HH:mm";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String YYYY_MM_DD_HH = "yyyy-MM-dd HH";

    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static String  yearToMonth(String dateString){
        // 将字符串解析为日期对象

        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return dateString;
        }

    // 格式化日期对象为新的字符串
        DateFormat newFormatter = new SimpleDateFormat("MM-dd");
        String newDateString = newFormatter.format(date);
        return newDateString;
    }

    public static String  yearToMonth1(String dateString){
        // 将字符串解析为日期对象

        DateFormat formatter = new SimpleDateFormat("yyyy-MM");
        Date date = null;
        try {
            date = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return dateString;
        }

    // 格式化日期对象为新的字符串
        DateFormat newFormatter = new SimpleDateFormat("MM");
        String newDateString = newFormatter.format(date);
        return newDateString+"月";
    }

    public static String formatTime(long time,String format){
        DateFormat formatter = new SimpleDateFormat(format);
        Date date = new Date(time);
       return formatter.format(date);
    }

    public static String  formatTimeStr(String dateString,String format){
        // 将字符串解析为日期对象

        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return dateString;
        }

        // 格式化日期对象为新的字符串
        DateFormat newFormatter = new SimpleDateFormat(format);
        String newDateString = newFormatter.format(date);
        return newDateString;
    }
    /**
     * 将2013:10:08 11:48:07如此格式的时间
     * 转化为毫秒数
     *
     * @param datetime 字符串时间
     * @return 毫秒数
     */
    public static long dateTimeToMSs(String datetime ,String format) {
        if (android.text.TextUtils.isEmpty(datetime)) return 0;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        final Date parse;
        try {
            parse = sdf.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
        return parse.getTime();
    }

    /**
     * 方法名：getDateToString(long milSecond)
     * 功    能：时间戳转成提示性日期格式（昨天、今天……)
     * 参    数：long milSecond
     * 返回值：String
     */
    public static String getDateToString(String datetime) {
        long milSecond = dateTimeToMSs(datetime,YYYY_MM_DD_HH_MM_SS);
        //现在时间
        String now = new SimpleDateFormat("yyyy-MM-dd ").format(milSecond);
        //昨天
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yday = new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());

        //今天
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 0);
        String today = new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
        //其他时间段
        Date date = new Date(milSecond);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");


        if (now.equals(today)) {
            return "今天 "+ formatTime(milSecond,HH_MM_Format);
        } else if (now.equals(yday)) {
            return "昨天 " + formatTime(milSecond,HH_MM_Format);
        } else {
            return format.format(date);
        }
    }

    /**
     * 方法名：getDateToString(long milSecond)
     * 功    能：时间戳转成提示性日期格式（昨天、今天……)
     * 参    数：long milSecond
     * 返回值：String
     */
    public static String getDateToString(long milSecond) {
        //现在时间
        String now = new SimpleDateFormat("yyyy-MM-dd ").format(milSecond);
        //昨天
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yday = new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
        //今天
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 0);
        String today = new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
        //其他时间段
        Date date = new Date(milSecond);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        if (now.equals(today)) {
            return "今天";
        } else if (now.equals(yday)) {
            return "昨天";
        } else {
            return format.format(date);
        }
    }

}
