You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于月份数值的日期归属判断及衍生日期生成技术问询

问题需求

给定两个输入日期,以及一组仅含月份数值(无年份)的数据集,数据集字段包括discount_start_month、discount_end_month、discount_code、advance_start_month、advance_end_month,需解决以下两个技术问题:

  • 无年份信息时,判断输入日期所属的discount_code月份区间;
  • 根据匹配到的discount_code对应的advance_start_month和advance_end_month,生成startDateGeneration和endDateGeneration两个衍生日期。

日期区间判断代码示例

private static boolean isInRange(
      LocalDate discountDate, Integer fromMonth, Integer toMonth) {
    YearMonth fromMonthDiscount = YearMonth.of(discountDate.getYear(), fromMonth);
    YearMonth toMonthDiscount = YearMonth.of(discountDate.getYear(), toMonth);
    YearMonth yearMonthDiscountDate =
        YearMonth.of(discountDate.getYear(), discountDate.getMonthValue());

    if (fromMonth > toMonth) {
      if (discountDate.getMonthValue() == Constants.DEC_MONTH) {
        toMonthDiscount = YearMonth.of(discountDate.getYear() + 1, toMonth);
      } else if (discountDate.getMonthValue() == Constants.JAN_MONTH) {
        fromMonthDiscount = YearMonth.of(discountDate.getYear() - 1, fromMonth);
      }
    }

    return (yearMonthDiscountDate.isAfter(fromMonthDiscount)
            || yearMonthDiscountDate.equals(fromMonthDiscount))
        && (yearMonthDiscountDate.isBefore(toMonthDiscount)
            || yearMonthDiscountDate.equals(toMonthDiscount));
  }

衍生日期生成逻辑代码示例

int startYear = fromDate.getYear();
    int endYear = toDate.getYear();
    
    // 从数据库中获取对应起始日期的advance_start_month
    int advanceStartMonth = ....
    
    if(advanceStartMonth >= Constants.OCT_MONTH &&
        advanceStartMonth <= Constants.DEC_MONTH &&
        startYear == endYear) {
        startYear = startYear - 1;
    }
    
    // 从数据库中获取对应结束日期的advance_start_month和advance_end_month  
    int discountResultStartMonth = //.......
    int discountResultEndMonth = //........
    
    if (discountResultStartMonth > discountResultEndMonth) {
        if (toDate.getMonthValue() == Constants.DEC_MONTH) {
            endYear = endYear + 1;
        }
    }

衍生日期构造代码示例

LocalDate startDateGeneration =
          fromDate
              .withMonth((Integer) fromDateSeasonMap.get(Constants.ADVANCE_START_MONTH))
              .withYear(startYear);
      LocalDate endDateGeneration =
          toDate
              .withMonth((Integer) toDateSeasonMap.get(Constants.ADVANCE_END_MONTH))
              .withYear(endYear)
              .withDayOfMonth(
                  YearMonth.of(endYear, (Integer) toDateSeasonMap.get(Constants.ADVANCE_END_MONTH))
                      .atEndOfMonth()
                      .getDayOfMonth());

注意事项

  • 数据集中不存在月份区间重叠的情况;
  • 所有月份字段仅为数值型数据;
  • 月份数值均不超过12。

预期输入输出案例

Begin Input
Start Date: 2023-08-01
End Date: 2023-10-01
startDateGeneration: 2023-06-01
endDateGeneration: 2023-11-30
===================================
Begin Input
Start Date: 2023-09-01
End Date: 2023-12-01
startDateGeneration: 2023-07-01
endDateGeneration: 2024-01-31
===================================
Begin Input
Start Date: 2023-01-30
End Date: 2023-02-01
startDateGeneration: 2022-11-30
endDateGeneration: 2023-03-31
===================================
Begin Input
Start Date: 2023-01-30
End Date: 2023-02-01
startDateGeneration: 2022-11-30
endDateGeneration: 2023-03-31
===================================
Begin Input
Start Date: 2022-10-30
End Date: 2023-02-01
startDateGeneration: 2022-08-30
endDateGeneration: 2023-03-31
===================================
Begin Input
Start Date: 2022-08-30
End Date: 2022-10-01
startDateGeneration: 2022-06-30
endDateGeneration: 2022-12-31
===================================
Begin Input
Start Date: 2022-08-30
End Date: 2023-02-01
startDateGeneration: 2022-06-30
endDateGeneration: 2023-03-31
===================================
Begin Input
Start Date: 2023-04-30
End Date: 2023-07-01
startDateGeneration: 2023-02-28
endDateGeneration: 2023-07-31
===================================
Begin Input
Start Date: 2023-06-30
End Date: 2023-07-01
startDateGeneration: 2023-04-30
endDateGeneration: 2023-07-31
===================================
Begin Input
Start Date: 2023-08-30
End Date: 2023-10-01
startDateGeneration: 2023-06-30
endDateGeneration: 2023-12-31
===================================
Begin Input
Start Date: 2023-10-30
End Date: 2024-01-01
startDateGeneration: 2023-08-30
endDateGeneration: 2024-03-31
===================================
Begin Input
Start Date: 2022-12-30
End Date: 2023-02-01
startDateGeneration: 2022-10-30
endDateGeneration: 2023-03-31
===================================
Begin Input
Start Date: 2023-01-30
End Date: 2023-02-01
startDateGeneration: 2022-10-30
endDateGeneration: 2023-03-31
===================================
Begin Input
Start Date: 2022-10-01
End Date: 2023-02-01
startDateGeneration: 2022-07-01
endDateGeneration: 2023-03-31
===================================
Begin Input
Start Date: 2022-12-01
End Date: 2023-02-01
startDateGeneration: 2022-10-01
endDateGeneration: 2023-03-31
===================================
Begin Input
Start Date: 2023-02-01
End Date: 2023-04-01
startDateGeneration: 2022-12-01
endDateGeneration: 2023-05-31
===================================
Begin Input
Start Date: 2023-02-01
End Date: 2023-03-01
startDateGeneration: 2022-12-01
endDateGeneration: 2023-03-31
===================================
Begin Input
Start Date: 2023-04-30
End Date: 2023-06-01
startDateGeneration: 2023-02-28
endDateGeneration: 2023-07-31
===================================
Begin Input
Start Date: 2023-06-30
End Date: 2023-08-01
startDateGeneration: 2023-04-30
endDateGeneration: 2023-08-31
===================================

内容的提问来源于stack exchange,提问作者oldcode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 04:02:02