本月1日和16日的半月付款频率的给定日期增加

问题描述 投票:-1回答:1

如果我知道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
 ...
java datetime java-8
1个回答
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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.