package com.zhimai.websocket.util;

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

/**
 * Explain：
 * Created on 2020/10/30.
 *
 * @author zhangshenglong
 */
public class TimeUtils {
    private static SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat simpleFormatter_day = new SimpleDateFormat("yyyy-MM-dd");

    public static String convertTimeStampToDate(String timeStamp) {
        Long time = new Long(timeStamp);
        time = time * 1000;
        return simpleFormatter.format(time);
    }

    public static String convertTimeStampToDate(int timeStamp) {
        Long time = new Long(timeStamp);
        time = time * 1000;
        return simpleFormatter.format(time);
    }

    public static String getTodayTimeStr() {
        Date curDate = new Date(System.currentTimeMillis());
        String str = simpleFormatter_day.format(curDate);
        return str;
    }

    /**
     * 获得指定日期的前一天
     *
     * @param specifiedDay
     * @return
     * @throws Exception
     */
    public static String getLastDay(String specifiedDay) {
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);

        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayBefore;
    }

    /**
     * 获得指定日期的后一天
     *
     * @param specifiedDay
     * @return
     */
    public static String getNextDay(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);

        String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayAfter;
    }

    public static String formateTime2(String time) {
        if (time.length() == 1)
            return 0 + time;

        return time;
    }
}
