获取上个月 这个月 下个月每天的日期和星期几

//可以获取周几
    //SimpleDateFormat sdf = new SimpleDateFormat("EEEE yyyy-MM-dd");

    public void getDateList() {
        //上个月
        Date lastMonth = getLastMonth();
        String format = DateUtil.format(lastMonth, "yyyy-MM-dd");
        DateTimeFormatter timeDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate sgy = LocalDate.parse(format, timeDtf);

        //这个月
        LocalDate today = LocalDate.now();
        LocalDate firstday = LocalDate.of(today.getYear(),today.getMonth(),1);
        Date zgydate = Date.from(firstday.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
        //下个月
        Date nextMonth = getNextMonth();
        String format2 = DateUtil.format(nextMonth, "yyyy-MM-dd");
        LocalDate xgy = LocalDate.parse(format2, timeDtf);

        //获取各个月有多少天
        int sgyConut = sgy.lengthOfMonth();
        int zgyConut = firstday.lengthOfMonth();
        int xgyConut = xgy.lengthOfMonth();

        List<Map<String, Object>> stringList = new LinkedList<>();

        //循环获取一个月的日期
        for (int i = 0; i < sgyConut; i++) {
            Map<String, Object> resultMap = new HashMap<>();
            Date newDate = DateUtil.offset(lastMonth, DateField.DAY_OF_MONTH, i);
            String format1 = DateUtil.format(newDate, "yyyy-MM-dd");
            String week = getWeek(format1);
            resultMap.put("week",week);
            String nowDate = DateUtil.format(newDate, "MM月dd日");
            resultMap.put("date",nowDate);
            String nowDate1 = DateUtil.format(newDate, "MM-dd");
            resultMap.put("id",nowDate1);
            stringList.add(resultMap);
        }
        for (int i = 0; i < zgyConut; i++) {
            Map<String, Object> resultMap = new HashMap<>();
            Date newDate = DateUtil.offset(zgydate, DateField.DAY_OF_MONTH, i);
            String format1 = DateUtil.format(newDate, "yyyy-MM-dd");
            String week = getWeek(format1);
            resultMap.put("week",week);
            String nowDate = DateUtil.format(newDate, "MM月dd日");
            resultMap.put("date",nowDate);
            String nowDate1 = DateUtil.format(newDate, "MM-dd");
            resultMap.put("id",nowDate1);
            stringList.add(resultMap);
        }
        for (int i = 0; i < xgyConut; i++) {
            Date newDate = DateUtil.offset(nextMonth, DateField.DAY_OF_MONTH, i);
            Map<String, Object> resultMap = new HashMap<>();
            String format1 = DateUtil.format(newDate, "yyyy-MM-dd");
            String week = getWeek(format1);
            resultMap.put("week",week);
            String nowDate = DateUtil.format(newDate, "MM月dd日");
            resultMap.put("date",nowDate);
            String nowDate1 = DateUtil.format(newDate, "MM-dd");
            resultMap.put("id",nowDate1);
            stringList.add(resultMap);
        }
        System.out.println(stringList);
    }

    private static Date getLastMonth() {
        //获取前一个月第一天
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, -1);
        calendar1.set(Calendar.DAY_OF_MONTH,1);
        Date time = calendar1.getTime();
        return time;
    }

    private static Date getNextMonth() {
        //获取下一个月第一天
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, 1);
        calendar1.set(Calendar.DAY_OF_MONTH,1);
        Date time = calendar1.getTime();
        return time;
    }

    //获取周几
    private static String getWeek(String weekWate){
        final String dayNames[] = { "周日", "周一", "周二", "周三", "周四", "周五","周六" };

        SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar = Calendar.getInstance();
        Date date = new Date();

        try {
            date = sdfInput.parse(weekWate);
        } catch (Exception e) {
            e.printStackTrace();
        }

        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
        if(dayOfWeek<0){
            dayOfWeek=0;
        }
        String dayName = dayNames[dayOfWeek];
        return dayName;
    }

需要引入hutool的jar包,一个强大的工具类

 <!--hutool工具类-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.9</version>
        </dependency>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值