Java获取一个月每周星期一至星期日的日期

2019-12-12  本文已影响0人  Rain_z

话不多说,直接上代码:

public class DateUtil {

    /**
     * 获取当月星期一到星期日的时间段
     * @param month
     * @return
     */
    public static List<Map<String,Object>> getWeekList(Date month){

        List<Map<String,Object>> list = getAllWeek(formatDate(month,"yyyy-MM"));
        //移除不是以星期一开始的时间段
        Map<String, Object> map = list.get(0);
        Date startDate = parseDate(map.get("begin").toString(),null);
        Date endDate = parseDate(map.get("end").toString(),null);
        if (endDate.getTime() - startDate.getTime() < (6*60*60*24*1000)){
            list.remove(0);
        }

        //解决星期一到星期天跨月问题
        Map<String, Object> map2 = list.get(list.size() - 1);
        Date startDate2 = parseDate(Const.getStrValue(map,"begin"),null);
        Date endDate2 = parseDate(Const.getStrValue(map,"end"),null);
        if (endDate2.getTime() - startDate2.getTime() < (6*60*60*24*1000)){
            long baseDate = startDate2.getTime() + (6 * 60 * 60 * 24 * 1000);
            Date date = new Date(baseDate);
            map2.put("end",formatDate(date,null));
        }

        //移除本周之后的时间段
        Iterator<Map<String, Object>> iterator = list.iterator();
        while (iterator.hasNext()){
            if (new Date().getTime() - parseDate(Const.getStrValue(iterator.next(),"begin"), null).getTime() < 0){
                iterator.remove();
            }
        }
        return list;
    }

    public static List<Map<String,Object>> getAllWeek(String dateStr){
        Date date = parseDate(dateStr,"yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, 1);//一个月的第几天,也就是几号,比如值为1,就代表是1号
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;//星期几,这个值在Calendar中有对应的常量比如,星期天就用:Calendar.SUNDAY,星期一就是Calendar.MONDAY
        dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek;
        int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);//当月天数
        List<Map<String,Object>> list = new ArrayList<>();
        HashMap<String, Object> map = null;
        for(int curDay = 1;curDay <= days;curDay++){
            c.set(Calendar.DAY_OF_MONTH, curDay);
            if(1 == curDay || 1 == dayOfWeek){
                map = new HashMap<>();
                map.put("begin",formatDate(c.getTime(),null));
            }
            if(dayOfWeek == 7 || curDay == days){
                map.put("end",formatDate(c.getTime(),null));
                list.add(map);
                dayOfWeek = 0;
            }
            dayOfWeek++;
        }
        return list;
    }


    public static String formatDate(Date date, String format){
        if(null == format || "".equals(format.trim())){
            format = "yyyy-MM-dd";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

参数为当前日期, 调用getWeekList()方法就可以了,为了展示效果,下面是循环打印了一下:

2019-12-02 ~ 2019-12-08
2019-12-09 ~ 2019-12-15

这个方式比较low,暂时性解决问题是可以的,其实很多处理在获取日期时就可以解决了, 如果你有更好的方式可以留言哦!

上一篇下一篇

猜你喜欢

热点阅读