每个月的第1和第16个半月支付频率的给定日期增量

时间:2019-02-27 15:51:49

标签: java datetime java-8

如果我知道对于semi-monthly payment frequency,应该始终使用每个月的1st16th(给定第一个日期),如何增加它?

这就是我所拥有的  远:

...
while(cnt.getAndIncrement() <= pmtNumber ) {
   monthdate = incrementDateUsingPaymentFrequency(LocalDate.of(2018, 2, 1), PaymentFrequencyCodeEnum.SEMIMONTHLY);
   //do something with this incremented month
}
...
public static LocalDate incrementDateUsingPaymentFrequency(LocalDate monthDate, PaymentFrequency paymentFrequency){
    LocalDate incrementedDate = null;
    if(paymentFrequency == PaymentFrequency.SEMIMONTHLY){
        incrementedDate = monthDate.plusDays(monthDate.getDayOfMonth() == 1 ? 16 : 0);
    }
    return incrementedDate;
}

我期望的结果:

 02/01/2018
 02/16/2018
 03/01/2018
 03/16/2018
 04/01/2018
 04/16/2018
 ...

1 个答案:

答案 0 :(得分:2)

因为我们知道对于semi-monthly仅使用1st16th。 当日期为每月1st时,只需增加15天。 还有什么时候(16th),请add 1 month填写日期并返回该月的1st天。

if(paymentFrequency == PaymentFrequency.SEMIMONTHLY){
            incrementedDate = monthDate.getDayOfMonth() == 1 ? monthDate.plusDays(15) : monthDate.plusMonths(1).withDayOfMonth(1);
    }