package com.ifavine.pay.utils;

/**
 * Created by Administrator on 2017/6/20.
 */

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

/**
 * Created by xyz on 2017/1/9.
 */

public class DateTimeUtils {
    private int hour;
    private int day;
    private Date date;
    private DateFormat df;
    private String pointText;
    private Long time;
    private Long now;


    public DateTimeUtils() {
        hour = 60 * 60 * 1000;
        day = (60 * 60 * 24) * 1000;
        time = 28800L;
        now = new Date().getTime();
        pointText = "";
    }

    //获得当天0点时间
    public static Long getTimesmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (Long) cal.getTimeInMillis();
    }


    //获取星期几
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    //获取时间点
    public String getTimePoint(String timeStr) {
        if (timeStr == null || "".equals(timeStr)) {
            timeStr = String.valueOf(new Date().getTime());
        }
        long time = Long.parseLong(timeStr) * 1000;
        //现在时间
        now = new Date().getTime();
        //时间点比当天零点早
        if (time <= now) {
            date = new Date(time);
            if (time < getTimesmorning()) {
                if (time >= getTimesmorning() - day) {//比昨天零点晚
                    pointText = "昨天";
                    return pointText;
                } else {
                    //显示具体日期
                    return date.getMonth() + 1 + "月" + date.getDate() + "日";
                }
            } else {//无日期时间,当天内具体时间
                Date d = new Date();
                int h = d.getHours() - date.getHours();
                int m = d.getMinutes() - date.getMinutes();
                if (h >= 1) {
                    return h + "小时前";
                } else {
                    if (m <= 5) {
                        return "刚刚";
                    }
                    return m + "分钟前";
                }
            }
        } else {
            pointText = "刚刚";
        }
        return pointText;
    }


    public static String getMeetingDate(String start, String end) {
        long time = Long.parseLong(start) * 1000;
        Calendar cal = Calendar.getInstance();//使用日历类
        cal.setTimeInMillis(time);
        int year = cal.get(Calendar.YEAR);//得到年
        int month = cal.get(Calendar.MONTH) + 1;//得到月，因为从0开始的，所以要加1
        int day = cal.get(Calendar.DAY_OF_MONTH);//得到天
        int hour = cal.get(Calendar.HOUR);//得到小时
        int minute = cal.get(Calendar.MINUTE);//得到分钟
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        String sTime = df.format(cal.getTime());
        String week = getWeekOfDate(cal.getTime());
        time = Long.parseLong(end) * 1000;
        cal.setTimeInMillis(time);
        String eTime = df.format(cal.getTime());
        return month + "月" + day + "日 " + week + " " + sTime + "~" + eTime;
    }

    public static String getMeetingDate(String start) {
        long time = Long.parseLong(start) * 1000;
        Calendar cal = Calendar.getInstance();//使用日历类
        cal.setTimeInMillis(time);
        int year = cal.get(Calendar.YEAR);//得到年
        int month = cal.get(Calendar.MONTH) + 1;//得到月，因为从0开始的，所以要加1
        int day = cal.get(Calendar.DAY_OF_MONTH);//得到天
        int hour = cal.get(Calendar.HOUR);//得到小时
        int minute = cal.get(Calendar.MINUTE);//得到分钟
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        String sTime = df.format(cal.getTime());
        String week = getWeekOfDate(cal.getTime());
        return month + "月" + day + "日 " + week + " " + sTime;
    }

}