package com.ruoyi.common.utils;

import java.time.*;

/**
 * @ClassName: TimeStampUtils
 * @Author: 于学智
 * @Description: 时间工具类
 * @CreateDate: 2023/9/16 12:26
 * @Version: 1.0
 * @E-mail:18722650553@139.com
 * @Link:https://github.com/18722650553
 */
public class TimeStampUtils {
    /**
     * 转成现在的年月日，时分秒还是之前的，秒毫秒为0
     *
     * @param timestamp
     * @return
     */
    public static LocalDateTime convertToTodayDateTime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        LocalDateTime previousDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        //获取现在时间
        LocalDate today = LocalDate.now();
        LocalTime previousTime = previousDateTime.toLocalTime();
        LocalDateTime todayDateTime = LocalDateTime.of(today, previousTime).withSecond(0).withNano(0);
        return todayDateTime;
    }

    /**
     * 转成现在的年月日，时分秒还是之前的，秒毫秒为0
     *
     * @param timestamp
     * @return
     */
    public static long convertToTodayDateTimeToMillis(long timestamp) {
        LocalDateTime previousDateTime = convertToTodayDateTime(timestamp);
        return convertToMillis(previousDateTime);
    }

    /**
     * LocalDateTime转毫秒时间戳
     *
     * @param dateTime
     * @return
     */
    public static long convertToMillis(LocalDateTime dateTime) {
        Instant instant = dateTime.atZone(ZoneId.systemDefault()).toInstant();
        return instant.toEpochMilli();
    }
}
