LocalDate 向后推几个月的日期如何计算

接了一个项目,其中要求在用户licence 有效日期前几个月发送邮件,一共三轮。这里就涉及到了日期的操作。

因此决定:先将当前日向后推n个月,n目前测试设定为1,2,3.

然后将符合的日期,放入到三个List中,再遍历每一个licence , 观察其截止日期是否在这几个日期的List之中。如果在,则当前日要发送邮件。

代码还是比较经典的。结构清晰,效率较高,是本菜思考了半天的结果。。。。。。

package com.msb.localDateTest;

import org.apache.tomcat.jni.Local;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;


public class FirstTest {


    public static void main(String[] args) {

        LocalDate current = LocalDate.of(2022,2,28);
        System.out.println(current);
        int first = 1;
        int second = 2;
        int third = 3;

        List<LocalDate> targetMonthDaysList_1st = new ArrayList<>();
        List<LocalDate> targetMonthDaysList_2nd = new ArrayList<>();
        List<LocalDate> targetMonthDaysList_3rd = new ArrayList<>();

        // frist month
        if(current.getDayOfMonth() > current.plusMonths(first).lengthOfMonth()){
            System.out.println("first month , out of range");
        }else if(current.getDayOfMonth() < current.lengthOfMonth()){
            System.out.println("putong zhuangtai");
            LocalDate temp = current.plusMonths(first);
            targetMonthDaysList_1st.add(temp);
        }else{
            int offset = 0;
            do{
                LocalDate temp = current.plusMonths(first).plusDays(offset);
                targetMonthDaysList_1st.add(temp);
                offset += 1;
            }while (current.getDayOfMonth() + offset <= current.plusMonths(first).lengthOfMonth());
        }

        if(current.getDayOfMonth() > current.plusMonths(second).lengthOfMonth()){
            System.out.println("second month , out of range");
        }else if(current.getDayOfMonth() < current.lengthOfMonth()){
            System.out.println("second,putong zhuangtai");
            LocalDate temp = current.plusMonths(second);
            targetMonthDaysList_2nd.add(temp);
        }else{
            int offset = 0;
            do{
                LocalDate temp = current.plusMonths(second).plusDays(offset);
                targetMonthDaysList_2nd.add(temp);
                offset += 1;
            }while (current.getDayOfMonth() + offset <= current.plusMonths(second).lengthOfMonth());
        }


        if(current.getDayOfMonth() > current.plusMonths(third).lengthOfMonth()){
            System.out.println("third month , out of range");
        }else if(current.getDayOfMonth() < current.lengthOfMonth()){
            System.out.println("the third month,putong zhuangtai");
            LocalDate temp = current.plusMonths(third);
            targetMonthDaysList_3rd.add(temp);
        }else{
            int offset = 0;
            do{
                LocalDate temp = current.plusMonths(third).plusDays(offset);
                targetMonthDaysList_3rd.add(temp);
                offset += 1;
            }while (current.getDayOfMonth() + offset <= current.plusMonths(third).lengthOfMonth());
        }

        System.out.println("diyige yuede yuefen");
        for(LocalDate localDate : targetMonthDaysList_1st){
            System.out.println(localDate);
        }
        System.out.println("dierge yuede yuefen");
        for(LocalDate localDate : targetMonthDaysList_2nd){
            System.out.println(localDate);
        }

        System.out.println("disange yue de yuefen");
        for(LocalDate localDate: targetMonthDaysList_3rd){
            System.out.println(localDate);
        }
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值