计算两个日期之间的工作日
程序员文章站
2022-03-03 10:09:11
...
public static int counter(String start, String end) throws ParseException {
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
Date start_date = dateFormat.parse(start);
Date end_date = dateFormat.parse(end);
int days = (int) ((end_date.getTime() - start_date.getTime())/(1000*3600*24) + 1);
int weeks = days/7;
int lest = days%7;
int count = weeks * 5;
if (lest > 0){
Calendar calendar=Calendar.getInstance();
calendar.setTime(end_date);
calendar.add(Calendar.DATE, 1 - lest);
int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
day_of_week = day_of_week-1>0 ? day_of_week-1:7;
int others = day_of_week + lest;
if (others < 7){
others = 0;
}
if (others == 7){
others = 1;
}
if (others > 7){
others = 2;
}
count += (lest-others);
}
return count;
}
转载于:https://my.oschina.net/buobao/blog/633087
上一篇: Vue3学习笔记
下一篇: 计算两日期之间的工作日天数(js写)