package cn.exploring.engine.server.core.util;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalUnit;

/**
 * 日期格式化工具类
 */
public class DateTimeUtil {

    /**
     * 格式 yyyy年MM月dd日 HH:mm:ss
     *
     * @param dateTime
     * @return
     */
    public static String getDateTimeDisplayString(LocalDateTime dateTime) {
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String strDate2 = dtf2.format(dateTime);

        return strDate2;
    }

    /**
     * 获取某天的开始时间
     * @param time
     * @return
     */
    public static LocalDateTime getBeginTimeOfDay(LocalDateTime time) {
        if (time == null) {
            time = LocalDateTime.now();
        }

        return LocalDateTime.of(time.toLocalDate(), LocalTime.MIN);
    }

    /**
     * 获取某天的结束时间
     * @param time
     * @return
     */
    public static LocalDateTime getdEnTimeOfDay(LocalDateTime time) {
        if (time == null) {
            time = LocalDateTime.now();
        }

        return LocalDateTime.of(time.toLocalDate(), LocalTime.MAX);
    }

    public static int diffTime(LocalDateTime t1, LocalDateTime t2) {
        Duration duration = Duration.between(t1,t2);
        long days = duration.toDays(); //相差的天数
        long hours = duration.toHours();//相差的小时数
        long minutes = duration.toMinutes();//相差的分钟数
        long millis = duration.toMillis();//相差毫秒数
        long nanos = duration.toNanos();//相差的纳秒数
        return 1;
    }

    //日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
    public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){
        return time.minus(number,field);
    }

}
