切分时间点常用工具类(静态生成点位key值,点位与时间点互转)

public class CommonUtils {

    
    private static final String  TIME_FORMAT = "HH:mm";

    /**
     * @description 获取时间段 将一天划分为 numIntervals 个时间点 numIntervals: 24 96 288 1440
     * @param numIntervals
     * @return
     */
    public static List<String>  getTimes(Integer numIntervals) {
        // 设置开始时间
        LocalTime startTime = LocalTime.of(0, 0);
        // 将一天划分为288个时间点  24 96 288 1440
        List<String> timePoints = new ArrayList<>();
        for (int i = 0; i < numIntervals; i++) {
            LocalTime timePoint = startTime.plusMinutes(i * (24 * 60) / numIntervals);
            timePoints.add(timePoint.format(DateTimeFormatter.ofPattern(TIME_FORMAT)));
        }
        return timePoints;
    }

    /**
     * @description 根据当前时间获取处于哪一个点位  totalPoints: 24 96 288 1440 可改造为自定义时间
     * @return
     */
    public static Integer getCurrentPoint(Integer totalPoints){
        LocalTime currentTime = LocalTime.now();
        int hours = currentTime.getHour();
        int minutes = currentTime.getMinute();
        int pointsPerHour = totalPoints / 24;
        int hourPoints = hours * pointsPerHour;
        int minutePoints = minutes / (60 / pointsPerHour);

        int currentPoint = hourPoints + minutePoints;
        System.out.println("当前时间点是96点中的第 " + currentPoint + " 个点。");
        return currentPoint;
    }

    /**
     * 根据输入点位获取相对应的时间
     * @param totalPoints 时间分割的维度
     * @param targetPoint 替换为你想要查询的点位
     * @return
     */
    public static String getCurrentDateTime(Integer totalPoints,Integer targetPoint){
        int pointsPerHour = totalPoints / 24;
        int hours = targetPoint / pointsPerHour;
        int minutes = (targetPoint % pointsPerHour) * (60 / pointsPerHour);
        LocalTime timeAtPoint = LocalTime.of(hours, minutes);
        System.out.println("点位 " + targetPoint + " 对应的时间是:" + timeAtPoint);
        return  timeAtPoint.format(DateTimeFormatter.ofPattern(TIME_FORMAT));
    }
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值