java简单的跳过工作日方法(只跳过周六周日)
public static void main(String[] args){
Date date = getData(new Date(), -7);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(date);
System.out.println(format);
}
public static Date getData(Date data,int index){
int step = index > 0 ? 1 : -1;
int i = 0;
int datAbs = Math.abs(index);
Calendar calendar = Calendar.getInstance();
calendar.setTime(data);
while(i < datAbs){
calendar.add(Calendar.DATE,step);
i++;
//如果需要跳过更多的日期,可以在if中添加条件
if (calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY ||
calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
i--;
}
}
return calendar.getTime();
}
}