package com.fs.watch.utils;



import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;
import java.time.ZoneId;

public class MyDateTimeUtils {

    public static String convert2DateTimeString(int seconds){
        Date dt = new Date();
        dt.setTime(seconds*1000L);
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+:00:00"));
        return sdf.format(dt);
    }

    public static Long getTimeDif(String starTime, String endTime,String type) {
        // 时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 将时间字符串转换为 LocalDateTime 对象
        LocalDateTime time1 = LocalDateTime.parse(starTime, formatter);
        LocalDateTime time2 = LocalDateTime.parse(endTime, formatter);

        // 计算时间差
        Duration duration = Duration.between(time1, time2);
        if ("second".equals(type)){
            return duration.getSeconds();
        }
        if ("minute".equals(type)){
            // 将时间差转换为分钟
            return duration.toMinutes();
        }
        if ("hour".equals(type)){
            return duration.toHours();
        }
        return null;
    }

    //线程安全
    public static Long getTimeDif(Date starTime, Date endTime, String type) {
        // 将Date对象转换为LocalDateTime对象
        LocalDateTime time1 = starTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime time2 = endTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

        // 计算时间差
        Duration duration = Duration.between(time1, time2);

        if ("second".equals(type)) {
            return duration.getSeconds();
        }
        if ("minute".equals(type)) {
            // 将时间差转换为分钟
            return duration.toMinutes();
        }
        if ("hour".equals(type)) {
            return duration.toHours();
        }
        return null;
    }
}
