zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    c语言网络编程过程及函数说明
    c代码编译完整过程详解
    const关键字的常见作用
    c语言中static关键字的几个作用
    c语言结构体中字节对齐方式
    modbus协议数据格式
    CodeForces
    如何在Dev-Cpp中使用C++11中的函数:stoi、to_string、unordered_map、unordered_set、auto
    关于lower_bound( )和upper_bound( )的常见用法
    CodeForces 600C——思维
  • 原文地址:https://www.cnblogs.com/theRhyme/p/9753408.html
Copyright © 2011-2022 走看看