【Java】取n工作日后的日期(仅排除周六周日)
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
public class HolidayUtils{
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static Date getTomorrow(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, +1);
date = calendar.getTime();
return date;
}
private static boolean isWeekend(String sdate) throws ParseException {
Date date = sdf.parse(sdate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
return true;
} else{
return false;
}
}
public static void main(String[] arg) throws ParseException{
Date today = new Date();
Date tomorrow = null;
int delay = 1;
int num = 3;//业务需要的n个工作日
while(delay <= num){
tomorrow = getTomorrow(today);
if (!isWeekend(sdf.format(tomorrow))){
delay++;
}
today = tomorrow;
}
LocalDateTime fDate = Instant.ofEpochMilli(today.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
//或LocalDateTime fDate = today.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()
String simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd").format(today);
System.out.println(simpleDateFormat);
}
}
作者:小魁jking
出处:https://www.cnblogs.com/wangjinkui/
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
出处:https://www.cnblogs.com/wangjinkui/
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任