java:求和LocalTimes差异

me-sa 阅读:32 2025-02-15 21:57:57 评论:0

我在 LocalTime 中有多个工作时间,我想检查时间是否超过 24 小时。
时间采用 24 小时格式。

例如:

  • 02:00 - 08:00
  • 10:00 - 12:00
  • 23:00 - 03:00

  • 在上面的示例中,它超过 24 小时,因为它从 02:00 开始一直持续到 03:00。
    只允许去到02:00。

    我实现了一个循环并尝试计算时间差并将其求和,例如:
  • 02:00 - 08:00 --> 6 小时
  • 08:00 - 10:00 --> 2 小时
  • 10:00 - 12:00 --> 2 小时
  • 12:00 - 23:00 --> 11 小时
  • 23:00 - 03:00 --> 4 小时

  • 总共是 25 小时,所以它大于 24 小时。
    但是我的问题是我无法计算 23:00 - 03:00 之间的时差,因为 LocalTime 只持续到 23:59:59。所以我目前无法计算超过 24 小时的持续时间。

    所以使用 ChronoUnit 是行不通的:
    ChronoUnit.HOURS.between(23:00, 03:00); 
    

    我不确定我应该使用什么样的方法来解决这个问题

    请您参考如下方法:

    如果您正在使用 LocalTime ,那么你必须使用 LocalTime.MINLocalTime.MAX用于关键时间段之间分钟的中间计算。你可以像在这个方法中那样做:

    public static long getHoursBetween(LocalTime from, LocalTime to) { 
        // if start time is before end time... 
        if (from.isBefore(to)) { 
            // ... just return the hours between them, 
            return Duration.between(from, to).toHours(); 
        } else { 
            /* 
             * otherwise take the MINUTES between the start time and max LocalTime 
             * AND ADD 1 MINUTE due to LocalTime.MAX being 23:59:59 
             */ 
            return ((Duration.between(from, LocalTime.MAX).toMinutes()) + 1 
                    /* 
                     * and add the the MINUTES between LocalTime.MIN (0:00:00) 
                     * and the end time 
                     */ 
                    + Duration.between(LocalTime.MIN, to).toMinutes()) 
                    // and finally divide them by sixty to get the hours value 
                    / 60; 
        } 
    } 
    

    你可以在 main 中使用它像这样的方法:
    public static void main(String[] args) { 
        // provide a map with your example data that should sum up to 24 hours 
        Map<LocalTime, LocalTime> fromToTimes = new HashMap<>(); 
        fromToTimes.put(LocalTime.of(2, 0), LocalTime.of(8, 0)); 
        fromToTimes.put(LocalTime.of(8, 0), LocalTime.of(10, 0)); 
        fromToTimes.put(LocalTime.of(10, 0), LocalTime.of(12, 0)); 
        fromToTimes.put(LocalTime.of(12, 0), LocalTime.of(23, 0)); 
        fromToTimes.put(LocalTime.of(23, 0), LocalTime.of(3, 0)); 
     
        // print the hours for each time slot 
        fromToTimes.forEach((k, v) -> System.out.println("from " + k + " to " + v  
                + "\t==>\t" + getHoursBetween(k, v) + " hours")); 
     
        // sum up all the hours between key and value of the map of time slots 
        long totalHours = fromToTimes.entrySet().stream() 
                .collect(Collectors.summingLong(e -> getHoursBetween(e.getKey(), e.getValue()))); 
     
        System.out.println("\ttotal\t\t==>\t" + totalHours + " hours"); 
    } 
    

    产生输出

    from 08:00 to 10:00 ==> 2 hours 
    from 23:00 to 03:00 ==> 4 hours 
    from 10:00 to 12:00 ==> 2 hours 
    from 02:00 to 08:00 ==> 6 hours 
    from 12:00 to 23:00 ==> 11 hours 
            total       ==> 25 hours 
    


    标签:java
    声明

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    关注我们

    一个IT知识分享的公众号