package com.junmp.jyzb.utils;

import cn.hutool.core.util.ObjectUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateTimeUtil {
    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static Date getCurrentDateTime() {
        Date currentDate = new Date();
        String dateString = DATE_FORMAT.format(currentDate);
        try {
            return DATE_FORMAT.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static List<Map<String,Object>> timeSort(List<Map<String,Object>> allPoliceman) {
        Collections.sort(allPoliceman, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Date updateTime1 = (Date) o1.get("updateTime");
                Date updateTime2 = (Date) o2.get("updateTime");
                return updateTime2.compareTo(updateTime1);
            }
        });
        return allPoliceman;
    }

    //时间戳转时间(Long->String)
    public static String TimeLongToString(Long time){
        //时间戳转时间Date
        if (ObjectUtil.isNotNull(time)){
            Date updateTime = new Date(time*1000);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date= format.format(updateTime);
            return date;
        }else {
            return null;
        }
    }

    //String->Long(将string类型的时间转成时间戳)
    public static Long TimeStringToLong(String time){
        if (ObjectUtil.isNotNull(time) && !time.trim().isEmpty()){
            Date date = null;
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date = sdf.parse(time);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            long timestamp = date.getTime();
            return timestamp;
        }else {
            return null;
        }
    }
    //Date->Long (将date类型转为Long)
    public static Long TimeDateToLong(Date time){
        if (ObjectUtil.isNotNull(time)){
            long time1 = time.getTime();
            return time1;
        }else {
            return null;
        }
    }

}