仅使用数值月查询月份内的日期

问题描述 投票:0回答:0

我有一个逻辑,其中有 2 个日期,我需要通过遍历仅包含月份信息的数据集来比较这些日期。

输入是开始日期结束日期

想法是通过按 seasonId 迭代此数据集来查询这些开始和结束日期在月份范围内的行。你会得到 101 或 100 不是所有的记录。

迭代每一行时调用此函数,

fromMonth
->
discount_start_month
// 来自数据集

toMonth
- >
discount_end_month
// 来自数据集

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));
  }

获取此数据时,基于

advance_start_month
我正在这样计算开始和结束年份,

advance_start_month
开始日期生成和
advance_end_month
生成结束日期

int startYear = fromDate.getYear();
int endYear = toDate.getYear();

// Get the advance_start_month from the DB iterating object of the start date
int advanceStartMonth = ....

if(advanceStartMonth >= Constants.OCT_MONTH &&
    advanceStartMonth <= Constants.DEC_MONTH &&
    startYear == endYear) {
    startYear = startYear - 1;
}

//// Get the advance_start_month and advance_end_month from the DB iterating object of the end date  
int discountResultStartMonth = //.......
    int discountResultEndMonth = //........

    if (discountResultStartMonth > discountResultEndMonth) {
        if (toDate.getMonthValue() == Constants.DEC_MONTH) {
            endYear = endYear + 1;
        }
    }

形成日期以从中生成范围的逻辑,

fromDateSeasonMap
-> 上述数据集中 1 行的数据集(开始日期)
toDateSeasonMap
-> 上述数据集中 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());

备注:

  1. 在任何给定的时间点,数据集中都不会有重叠的月份。
  2. 只有月份的数字数据存在。
  3. 不会有大于 12 的值,因为一年只有 12 个月。

我的问题:

  1. 我如何动态处理

    isInRange
    的逻辑,而不是从 11 月(11 日)到 2 月(2 日)这样的年份之间的月份?

  2. 如何处理基于上述数据集的年份生成逻辑?现在这个数据是硬编码的

预期输入和输出

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
===================================
java date localdate yearmonth
© www.soinside.com 2019 - 2024. All rights reserved.