有多少个工作日,有多少天,有多少天的周末"/>
两个时间之间的有多少个工作日,有多少天,有多少天的周末
不愿说废话直接上代码
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");int count = 0;public static void main(String[] args) throws ParseException {String sTime = "2020-12-28";String eTime = "2021-02-12";List gongzuori = gongzuori(sTime, eTime);for (Object o : gongzuori) {System.out.println(o.toString());}}public static List gongzuori(String sTime, String eTime) throws ParseException {//工作日计数器int count = 0;//周末计数器int count2 = 0;//开始时间转格式Date parse = sdf.parse(sTime);//结束时间转格式Date parse2 = sdf.parse(eTime);//中间相差多少天int time = (int) ((parse2.getTime() - parse.getTime()) / 1000 / 60 / 60 / 24);ArrayList<Object> list = new ArrayList<>();Calendar calendar = Calendar.getInstance();for (int i = 0; i < time; i++) {//获取第i天的日期calendar.setTime(new Date());calendar.add(Calendar.DAY_OF_MONTH, +i);Date date = calendar.getTime();//获取本周第几天int i1 = calendar.get(Calendar.DAY_OF_WEEK);//1是周日 7是周六if (i1 == 1 || i1 == 7) {count2++;list.add("时间:" + sdf.format(date) + "是周末");} else {count++;list.add("时间:" + sdf.format(date) + "不是周末");}list.add("工作日共有" + count+"天");list.add("周末共有" + count2+"天");list.add("之间共有"+time+"天");}return list;}
更多推荐
两个时间之间的有多少个工作日,有多少天,有多少天的周末
发布评论