接了一个项目,其中要求在用户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);
}
}
}