个人使用更新之后的 两个日期之前的 工作日计算 排除节假日和周六日(谁有更好的方法可以让我借鉴一下)
import org.omg.Messaging.SyncScopeHelper;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
-
@author :Wrry
-
@desc :
-
@date 2021/7/9 14:44
*/
public class scjssj {public static void hqjgrq(String date, int ts) throws ParseException {
//格式转换 转换成LocalDate 时间格式 (起始日期)
LocalDate dateOfBirth = LocalDate.parse(date);
//定义一个记次变量
int days = 1;
// 定义死循环知道 ts = i的时候跳出循环
for (int i = 1; i <= ts; ) {
//获取天数
Period of = Period.of(0, 0, days);
days++;
//天数字符转为LocalDate (当前时间+ 天数)
LocalDate DateTime = dateOfBirth.plus(of);// 获取周 String dayOfWeekName = DateTime.getDayOfWeek().toString(); //创建一个工作日集合 List<LocalDate> lists=new ArrayList<>(); // 记录 工作日 if (!(dayOfWeekName.equals("SATURDAY") || dayOfWeekName.equals("SUNDAY"))) { i++; lists.add(DateTime); if (i == ts) { System.out.println(DateTime); ceshiteshuriz(DateTime,lists,ts); } } }
}
public static void ceshiteshuriz(LocalDate dateTime, List qcjq,int ts) {
// DateTime
List list = Arrays.asList(“2021-01-01”, “2021-01-02”, “2021-01-03”,
“2021-02-12”, “2021-02-13”, “2021-02-14”, “2021-02-15”, “2021-02-16”,
“2021-02-17”, “2021-04-03”, “2021-04-04”, “2021-04-05”, “2021-05-01”,
“2021-05-02”, “2021-05-03”, “2021-05-04”, “2021-05-05”);//处理时间戳格式化日期 第一个参数 时间戳转日期 第二个参数 定义纳秒级 第三个参数 时区,时间偏移量 //LocalDateTime time2 =LocalDateTime.ofEpochSecond(timestamp/1000,0,ZoneOffset.ofHours(8)); //创建一个List<LocalDate> 数组 存放工作日的值 List<LocalDate> listrqgs = new ArrayList<>(); for (String lits : list){ LocalDate endOfFeb = LocalDate.parse(lits); listrqgs.add(endOfFeb); } //去从 List<LocalDate> C = new ArrayList<LocalDate>(); C.addAll(qcjq); C.removeAll(listrqgs); int ys =ts-(ts- C.size()); System.out.println(ts+"ts的值"); System.out.println(C); System.out.println(ys+"ys的值"); zuizhong(dateTime,ys);
}
public static void zuizhong(LocalDate dateTime , int ys){
int days = 1;
// 循环 添加接下来一年的时间
for (int i = 0; i < ys; ) {
Period of = Period.of(0, 0, days);
days++;
LocalDate DateTime = dateTime.plus(of);// 获取周 String dayOfWeekName = DateTime.getDayOfWeek().toString(); //System.out.println(dayOfWeekName); // 默认0 当为休息日时 1 int temp = 0; List<LocalDate> lists=new ArrayList<>(); // 记录 工作日 if (!(dayOfWeekName.equals("SATURDAY") || dayOfWeekName.equals("SUNDAY"))) { i++; lists.add(DateTime); if (i == ys) { System.out.println("222222222222"+DateTime); //ceshiteshuriz(DateTime,lists,ys); } } }
}
public static void main(String[] args) throws ParseException {
hqjgrq(“2021-01-01”,61);
}
}
/* // 日期实体类
Holiday holiday = new Holiday();
holiday.setRepDate(Date.from(DateTime.atStartOfDay(ZoneOffset.ofHours(8)).toInstant()));
holiday.setRepYear(Year);
holiday.setRepMonth(Month);
holiday.setRepDay(day);
holiday.setRepWeek((int) (w / 2));
holiday.setDbStatus(temp);
holidays.add(holiday);*/
/* // 获取年份
int Year = DateTime.getYear();
// 获取月份
int Month = DateTime.getMonthValue();
// 获取日分
int day = DateTime.getDayOfMonth();*/
// 当前为第几周
//int w = 2;
// 设置初始时间
//LocalDate dateOfBirth = LocalDate.of(2021, 1, 1);
//ArrayList holidays = new ArrayList<>();