• Java8给出一个时间段,计算该时间范围由哪些日期(天)和月份组成


    1. 判断时间段是否合法;

    2. 循环判断记录数是否大于0

    3. 根据起始时间算出该月的第一天、最后一天和这个月有多少天;

    4. 判断起始时间是否是该月第一天,如果是,再判断结束时间与该月最后一天的大小:小于,等于,大于;

    5. 判断起始时间是否是该月第一天,如果不是,再判断结束时间与该月最后一天的大小:小于,等于,大于;

    这里的LocalDate是Java8中的时间类,只有日期,没有时分秒等;如2018-03-26。

    下面函数仅仅只需要接受两个LocalDate参数(一个时间段),就可以获得该时间段内由哪些日期和哪些月份组成;

    Date、long转LocalDate等更多时间常用操作可以参考:https://www.cnblogs.com/theRhyme/p/9756154.html

    具体代码如下:

    public void accordingToCountCalcData(LocalDate start,LocalDate end){

        //这里的count表示自定义时间段内需要累加的记录的条数 final long count = end.toEpochDay() - start.toEpochDay() + 1; if(count < 1){ log.error("startTime can't greater than endTime",new IllegalArgumentException("startTime can't greater than endTime")); return; } log.info("******BEGIN: There are {} records between {} and {}. ******",count,start,end); long num = count; while(num > 0) { LocalDate firstDay = start.with(TemporalAdjusters.firstDayOfMonth()); LocalDate lastDay = start.with(TemporalAdjusters.lastDayOfMonth()); //该月有多少天 int dayOfMonth = lastDay.getDayOfMonth(); if (firstDay.compareTo(start) == 0) { //传入的起始时间如果是当月的第一天 if (lastDay.compareTo(end) == 0) { //传入的结束时间和开始时间构成了完整一个月 num = num - lastDay.getDayOfMonth(); log.info("{} and {} which making up a full month",firstDay,lastDay); } else if (end.compareTo(lastDay) > 0) { //包含了完整的一个月 log.info("Accumulating {} to {} , {} records",start,lastDay,dayOfMonth); num = num - dayOfMonth; //下个月的这天 start = start.minusMonths(-1); //下个月的1号 start = LocalDate.of(start.getYear(),start.getMonthValue(),1); } else if (end.compareTo(lastDay) < 0) { log.info("Accumulating {} to {} , {} records.",start,end,num); num = 0; } }else if(start.compareTo(firstDay) > 0) { //传入的开始时间不是当前月的第一天 if (end.compareTo(lastDay) <= 0) { log.info("Accumulating {} to {} , {} records.", start, end, num); num = 0; } else if (end.compareTo(lastDay) > 0) { //先累加从开始时间到这个月最后一天的记录 Period p = Period.between(start, lastDay); //这里还减1是因为两个日期如果相差11天,就有12条纪录 int records = p.getDays() + 1; log.info("Accumulating {} to {} , {} records.", start, lastDay, records); num = num - records; //下个月的这天 start = start.minusMonths(-1); //下个月的1号 start = LocalDate.of(start.getYear(), start.getMonthValue(), 1); } } } log.info("****** End: Function "accordingToCountCalcData". ******"); }

    测试结果如下:

    在线日期工具:

    https://tool.lu/timestamp/

    http://www.ab126.com/clock/1639.html

    https://bjtime.cn/riqi/

  • 相关阅读:
    Leetcode 122. Best Time to Buy and Sell Stock II
    Leetcode 337. House Robber III
    Leetcode 213. House Robber II
    java 常用正则表达式总结
    eclipse异常解决:Errors occurred during the build.........
    关于匿名内部类的理解!
    接口/抽象类的思考
    IO流对文件的读取操作
    标准类的建立,以及用集合对标准类中的元素进行存取操作
    利用集合进行对元素的添加和删除
  • 原文地址:https://www.cnblogs.com/theRhyme/p/9753408.html
Copyright © 2020-2023  润新知