package com.taikor.investment.utils;

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

/**
 * 时间格式转化工具类
 */
public class TimeUtils {

    private static final SimpleDateFormat DEFAULT_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    private static final int DAY = 60 * 60 * 1000 * 24;
    private static final int HOUR = 60 * 60 * 1000;
    private static final int MIN = 60 * 1000;
//    private static final int SEC = 1000;

    //将时间戳转换成指定格式的字符串
    public static String getStringDate(long time) {
        return DATE_FORMAT.format(new Date(time * 1000L));
    }

    //将时间戳转换成指定格式的字符串
    public static String getStringTime(long time) {
        return DEFAULT_FORMAT.format(new Date(time * 1000L));
    }

    //将时间字符串转成时间戳
    public static long stringToLong(String time) {
        try {
            Date date = DEFAULT_FORMAT.parse(time);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //转换时间
    public static String getDate(long time) {

        //获取当前时间
        String currentTime = DEFAULT_FORMAT.format(new Date());
        String serverTime = getStringTime(time);
        try {
            Date d1 = DEFAULT_FORMAT.parse(currentTime);
            Date d2 = DEFAULT_FORMAT.parse(serverTime);

            long duration = d1.getTime() - d2.getTime();
            //计算天数
            long day = duration / DAY;
            // 计算小时数
            long hour = duration / HOUR;
            // 计算分钟数
            long min = duration % HOUR / MIN;
            // 计算秒数
//            long second = duration % MIN / SEC;

            if (day == 0) {
                if (hour > 0) {
                    return hour + "小时前";
                } else {
                    return min + "分钟前";
                }
            } else {
                if (day > 7) {
                    return "一周前";
                } else {
                    return day + "天前";
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
