首页 > 其他分享 >LocalTime 持续时长计算

LocalTime 持续时长计算

时间:2023-06-09 16:01:49浏览次数:50  
标签:return LocalTimeDuration 持续 start result 计算 end LocalTime

public class DurationUtil {
    private static final Logger log = LoggerFactory.getLogger(DurationUtil.class);

    public static double toHours(Temporal start, Temporal end) {
        long minutes = Duration.between(start, end).toMinutes();
        long hours = minutes / 60;
        double v = (minutes % 60) / 60D;
        return v + hours;
    }


    /**
     * 24小时内 两个时间段的重合区间
     * <pre>
     *     public static void main(String[] args) {
     *         {
     *             LocalTime formatStart = LocalTime.of(1, 0);
     *             LocalTime formatEnd = LocalTime.of(8, 0);
     *             LocalTime compareStart = LocalTime.of(21, 0);
     *             LocalTime compareEnd = LocalTime.of(8, 0);
     *             double v = toMillis(formatStart, formatEnd, compareStart, compareEnd) / 1000 / 60;
     *             System.out.println("shout 180 current is " + v);
     *         }
     *         {
     *             LocalTime formatStart = LocalTime.of(21, 0);
     *             LocalTime formatEnd = LocalTime.of(8, 0);
     *             LocalTime compareStart = LocalTime.of(21, 0);
     *             LocalTime compareEnd = LocalTime.of(8, 3);
     *             double v = toMillis(formatStart, formatEnd, compareStart, compareEnd) / 1000 / 60;
     *             System.out.println("shout 3 current is " + v);
     *         }
     *         {
     *             LocalTime formatStart = LocalTime.of(21, 0);
     *             LocalTime formatEnd = LocalTime.of(8, 0);
     *             LocalTime compareStart = LocalTime.of(20, 56);
     *             LocalTime compareEnd = LocalTime.of(8, 3);
     *             double v = toMillis(formatStart, formatEnd, compareStart, compareEnd) / 1000 / 60;
     *             System.out.println("shout 7 current is " + v);
     *         }
     *         {
     *             LocalTime formatStart = LocalTime.of(4, 0);
     *             LocalTime formatEnd = LocalTime.of(8, 0);
     *             LocalTime compareStart = LocalTime.of(20, 56);
     *             LocalTime compareEnd = LocalTime.of(8, 3);
     *             double v = toMillis(formatStart, formatEnd, compareStart, compareEnd) / 1000 / 60;
     *             System.out.println("shout  current is " + v);
     *         }
     *         {
     *             LocalTime formatStart = LocalTime.of(21, 0);
     *             LocalTime formatEnd = LocalTime.of(8, 0);
     *             LocalTime compareStart = LocalTime.of(20, 30);
     *             LocalTime compareEnd = LocalTime.of(4, 0);
     *             double v = toMillis(formatStart, formatEnd, compareStart, compareEnd) / 1000 / 60;
     *             System.out.println("shout 30 current is " + v);
     *         }
     *         {
     *             LocalTime formatStart = LocalTime.of(21, 0);
     *             LocalTime formatEnd = LocalTime.of(8, 0);
     *             LocalTime compareStart = LocalTime.of(21, 30);
     *             LocalTime compareEnd = LocalTime.of(4, 0);
     *             double v = toMillis(formatStart, formatEnd, compareStart, compareEnd) / 1000 / 60;
     *             System.out.println("shout 0 current is " + v);
     *         }
     *         {
     *             LocalTime formatStart = LocalTime.of(21, 0);
     *             LocalTime formatEnd = LocalTime.of(8, 0);
     *             LocalTime compareStart = LocalTime.of(20, 30);
     *             LocalTime compareEnd = LocalTime.of(20, 50);
     *             double v = toMillis(formatStart, formatEnd, compareStart, compareEnd) / 1000 / 60;
     *             System.out.println("shout 20 current is " + v);
     *         }
     *     }
     * </pre>
     */
    public static long toMillis(LocalTime formatStart, LocalTime formatEnd, LocalTime compareStart, LocalTime compareEnd) {
        List<LocalTimeDuration> build = LocalTimeDuration.build(formatStart, formatEnd);
        LocalTimeDuration formatMin = build.get(0);
        LocalTimeDuration formatMax = build.get(1);
        List<LocalTimeDuration> compares = LocalTimeDuration.build(compareStart, compareEnd);
        long result = 0;
        for (LocalTimeDuration compare : compares) {
            result += toMillis(formatMin, formatMax, compare);
        }
        return result;
    }

    private static long toMillis(LocalTimeDuration min, LocalTimeDuration max, LocalTimeDuration compare) {
        if (compare == LocalTimeDuration.empty) {
            return 0;
        }
        LocalTime start = compare.start;
        LocalTime end = compare.end;
        long result = 0;
        if (min != LocalTimeDuration.empty) {
            if (start.isBefore(min.start)) {
                if (end.isBefore(min.start)) {
                    return Duration.between(start, end).toMillis();
                } else {
                    result += Duration.between(start, min.start).toMillis();
                    start = min.end;
                }
            }
            if (min.isIn(start)) {
                start = min.end;
            }
        }

        if (start.isAfter(end)) {
            return result;
        }
        if (start.isBefore(max.start)) {
            if (end.isBefore(max.start)) {
                return result + Duration.between(start, end).toMillis();
            } else if (max.isIn(end)) {
                return result + Duration.between(start, max.start).toMillis();
            } else {
                result += Duration.between(start, max.start).toMillis();
                start = max.end;
            }
        } else if (max.isIn(start)) {
            if (end.isBefore(max.end)) {
                return result;
            } else {
                return result + Duration.between(max.end, end).toMillis();
            }
        } else {
            return result + Duration.between(start, end).toMillis();
        }
        if (start.isAfter(end)) {
            return result;
        }
        return result + Duration.between(start, end).toMillis();
    }


    /**
     * eg: start-> 21:00 end -> 08:00 checkTime -> 22:00 result is true
     * eg: start-> 21:00 end -> 08:00 checkTime -> 21:00 result is true
     * eg: start-> 21:00 end -> 08:00 checkTime -> 08:00 result is true
     * eg: start-> 21:00 end -> 08:00 checkTime -> 04:00 result is true
     * eg: start-> 21:00 end -> 08:00 checkTime -> 08:30 result is false
     * eg: start-> 08:00 end -> 12:00 checkTime -> 08:30 result is true
     * eg: start-> 08:00 end -> 12:00 checkTime -> 11:30 result is true
     * eg: start-> 08:00 end -> 16:00 checkTime -> 13:30 result is true
     * eg: start-> 08:00 end -> 16:00 checkTime -> 17:30 result is false
     */

    public static boolean timeInDuration(LocalTime start, LocalTime end, LocalTime time) {
        if (log.isDebugEnabled()) {
            log.debug("start [{}] end [{}] time[{}]", start, end, time);
        }
        List<LocalTimeDuration> durations = LocalTimeDuration.build(start, end);
        for (LocalTimeDuration duration : durations) {
            if (duration == LocalTimeDuration.empty) {
                continue;
            }
            if (duration.isIn(time)) {
                return true;
            }
        }
        return false;
    }

    private static class LocalTimeDuration {
        private static final LocalTimeDuration empty = new LocalTimeDuration(null, null);

        public final LocalTime start;
        public final LocalTime end;

        public LocalTimeDuration(LocalTime start, LocalTime end) {
            this.start = start;
            this.end = end;
        }

        public static LocalTimeDuration of(LocalTime start, LocalTime end) {
            return new LocalTimeDuration(start, end);
        }

        public static LocalTimeDuration max(LocalTime time) {
            return new LocalTimeDuration(time, LocalTime.MAX);
        }

        public static LocalTimeDuration min(LocalTime time) {
            return new LocalTimeDuration(LocalTime.MIN, time);
        }

        public static List<LocalTimeDuration> build(LocalTime start, LocalTime end) {
            if (start.isAfter(end)) {
                return Arrays.asList(min(end), max(start));
            }
            return Arrays.asList(empty, of(start, end));
        }

        /**
         * 全闭
         */
        public boolean isIn(LocalTime time) {
            //return start.isBefore(time) && end.isAfter(time);
            return time.equals(start) || (start.isBefore(time) && end.isAfter(time)) || end.equals(time);
        }

        public String toString() {
            if (start != null && end != null) {
                return start + "\t" + end;
            }
            return "";
        }
    }
}

标签:return,LocalTimeDuration,持续,start,result,计算,end,LocalTime
From: https://www.cnblogs.com/zzzz-yh/p/17469430.html

相关文章

  • 计算机体系结构属性
     《编码》 信息源熵是一种上限,一般来说是不可能达到的其用来当作是一种标准 《Huffman》 这点告诉我们,在二进制数编码的情况下,Huffman编码是最优的,即平均码长可以达到最短 《长度固定编码法 》  《扩展编码法》   这里的1-2-3-5是指出扩展之后指令......
  • 非科班自学计算机需要学习什么内容?
    文章目录前言一、方向>语言的选择1.1语言vs方向1.2重要观点!二、自学方法另外说到计算机相关基础推荐书籍:三、自学资源前言非计算机专业,又想通过自学找到计算机相关工作的同学还是很多的。并且这条路也是可行的,毕竟计算机专业的同学也要自学。一、方向>语言的选择其实在校生如果......
  • 锂电池尺寸与型号、容量计算
    电池(蓝牙耳机、充电盒上使用)尺寸与型号关系如电池型号:801544(电池长44mm宽15mm 高8.0mm)注意中间两位数字是电池出线的边,这个型号是电池保护板在15mm的“边”上面。如电池型号:682723(电池长23mm宽27mm 高6.8mm) 这个型号是电池保护板在27mm的“边”上面。 已知电池......
  • 展会ING丨计讯物联在中国水博览会大放异彩,现场人气持续狂飙
    6月7日,由中国水利学会和中国水利工程协会联合打造的的2023中国水博览会暨第十八届中国(国际)水务高峰论坛于江苏·南京国际展览中心盛大举行,超过40多个国家和地区的2450余家展商如约而至,超24万人次的专业观众齐聚一堂,水利部及政府相关领导、知名院士专家荟萃于此,释放出巨大能量,展会......
  • 从宏基因组测序数据生成宏基因组组装基因组的计算工具
    从宏基因组测序数据生成宏基因组组装基因组的计算工具小组成员及分工王嘉璐22020080046:负责摘要、引言部分王涵22020080045:负责用于构建mag的上游分析工具部分王婷22020080047:负责总结,查找文献,博文整理汇总 1摘要微生物本质上与地球上的人类生活有着错综复杂的联系。......
  • 【计算机视觉】基于纹理特征的指纹识别位置与姿态算法
    简介计算机视觉基于纹理特征的指纹识别中的位置和姿态估计是指确定指纹图像中指纹的位置和方向的过程。这些信息对于后续的特征提取和匹配非常重要。下面介绍两个常用的算法来实现位置和姿态的估计:方向图和边界框。方向图(OrientationMap)方向图(OrientationMap)是用来表示指纹图像中......
  • 计算机图形学与GPU渲染 -- 什么是计算机图形学
    一:定义 关于计算机图形学的定义众说纷纭。IEEE对计算机图形学的定义为:Computergraphicsistheartorscienceofproducinggraphicalimageswiththeaidofcomputer。 国际标准化组织ISO将计算机图形学定义为:计算机图形学是一门研究通过计算机将数据转换成图形,并......
  • QA|如何实现一个函数读取1-9按钮?|网页计算器自动化测试实战
    如何实现一个函数读取1-9按钮?如何实现一个根据我们Json存的数字,自动选取对应按钮点击,并且点击对应算法?首先我们肯定不能给每个元素都写一个函数吧,毕竟挺多,而且如果这样写了,那加减乘除都要分开测试,代码太冗余,此时我们发现数字按钮的定位ID都很类似,如下   于是我们可以写成......
  • 一文读懂大厂面试的计算机网络面试题目(超详细整理)(TCP/IP,OSI,HTTP协议)
    对于大厂的面试来说,掌握基本的计算机网络知识十分必要,但是说实话就单单是博主觉得,看书去复习,是最好的“安眠药”,哈哈哈,所以具有针对性的去学习更加的有效果,所以直接看大厂的高频面试题,快速建立知识结构体系。以下的一些是博主通过博览众多平台的博客推文进行的汇总:1.计算机网络OS......
  • 第一章:计算机系统概论--第一轮
    教材说明:计算机组成原理(白中英),第六版,目前只是在复习阶段重做要求过的题目,第一行是我的答案,红色是没写的答案;另:附有思维导图课后习题:1.比较电子数字计算机和电子模拟计算机的特点解:电子数字计算机是按位运算的,并且不连续地跳动计算;用数字0和1表示数据,采用数字计数的计算方式,程序......