package com.sptg.common.core.util;

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

/**
 * title: DateUtil
 * package: com.sptg.common.core.util
 * description:Date工具类
 *
 *                
 *               
 */
public class DateUtil {

    /**
     * 功能描述:上周周一
     * @return Date
     *                
     *                      
     */
    public static Date getBeginDayOfLastWeek(){
        Date date = new Date();

        // 取上周一 00:00:00
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date);
        //-7上周
        cal1.add(Calendar.DATE, -7);
        cal1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal1.set(Calendar.HOUR_OF_DAY, 0);
        cal1.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.SECOND, 0);
        return  cal1.getTime();
    }


    /**
     * 功能描述:本周周一
     * @return Date
     *                
     *                      
     */
    public static  Date getBeginDayOfWeek() {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return  cal.getTime();
    }

    /**
     * 由过去的某一时间,计算距离当前的时间
     */
    public static String calculateTime(Date date) {
        String msg="";
        // 获取当前时间的毫秒数
        long nowTime = System.currentTimeMillis();
        // 指定时间
        Date setTime = date;
        // 获取指定时间的毫秒数
        long reset = setTime.getTime();
        long dateDiff = nowTime - reset;
        if (dateDiff < 0) {
            msg = "输入的时间不对";
        } else {
            // 秒
            long dateTemp1 = dateDiff / 1000;
            // 分钟
            long dateTemp2 = dateTemp1 / 60;
            // 小时
            long dateTemp3 = dateTemp2 / 60;
            // 天数
            long dateTemp4 = dateTemp3 / 24;
            // 月数
            long dateTemp5 = dateTemp4 / 30;
            // 年数
            long dateTemp6 = dateTemp5 / 12;
            if (dateTemp6 > 0) {
                msg = dateTemp6 + "年前";
            } else if (dateTemp5 > 0) {
                msg = dateTemp5 + "个月前";
            } else if (dateTemp4 > 0) {
                msg = dateTemp4 + "天前";
            } else if (dateTemp3 > 0) {
                msg = dateTemp3 + "小时前";
            } else if (dateTemp2 > 0) {
                msg = dateTemp2 + "分钟前";
            } else if (dateTemp1 > 0) {
                msg = "刚刚";
            }
        }
        return msg;
    }

    /**
     * 功能描述:获取某年第几周,跟mysql计算YEARWEEK匹配
     * @param date
     * @param firstDayOfWeek //设置为一周的第一天  如：Calendar.MONDAY
     * @return int
     *                
     *                      
     */
    public static  int getYearWeek(Date date,int firstDayOfWeek){
        Calendar calendar= Calendar.getInstance();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(firstDayOfWeek);
        int year=calendar.get(Calendar.YEAR);
        int yearWeek=calendar.get(Calendar.WEEK_OF_YEAR);
        int month=calendar.get(Calendar.MONTH)+1;
        //特殊处理12月份第一周是下一年 采用mysql YEARWEEK函数处理方法
        if(month==12&&yearWeek==1){
            year=year+1;
        }
        String yearWeekStr=String.format("%02d",yearWeek);
        return Integer.parseInt(year+yearWeekStr);
    }
}
