java生成当前最近的12个月份的起始时间点

114 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

  //封装 键 日期 当月开始时间点  当月结束时间点 
    public List<Map<String,Object>> getList12(){
        //获取最近12个月
        List<String> last12Months = getLast12Months();
        List<Map<String,Object>> list = new ArrayList<>();
        for(Integer i=0;i<last12Months.size();i++){
            Map<String,Object> tempMap = new HashMap<>();

            tempMap.put("key",i);
            tempMap.put("date",last12Months.get(i));
            String[] split = last12Months.get(i).split("-");
            tempMap.put("start",getBeginTime(Integer.parseInt(split[0]),Integer.parseInt(split[1])));
            tempMap.put("end",getEndTime(Integer.parseInt(split[0]),Integer.parseInt(split[1])));
            list.add(tempMap);
        }
        return list;
    }

    /**
     *
     * @return 返回最近12个月的日期
     */
    public List<String> getLast12Months(){
        ArrayList<String> latest12Months = new ArrayList<>(12);
        LocalDate today = LocalDate.now();
        for(long i = 11L;i >=0L; i--){
            LocalDate localDate = today.minusMonths(i);
            String ss = localDate.toString().substring(0,7);
            latest12Months.add(ss);
        }
        return latest12Months;
    }

    /**
     * 根据 年 月 获取 开始时间
     * @param year
     * @param month
     * @return
     */
    public Date getBeginTime(int year, int month) {
        YearMonth yearMonth = YearMonth.of(year, month);
        LocalDate localDate = yearMonth.atDay(1);
        LocalDateTime startOfDay = localDate.atStartOfDay();
        ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));
        return Date.from(zonedDateTime.toInstant());

    }

    /**
     * 根据 年 月 获取 结束时间
     * @param year
     * @param month
     * @return
     */
    public Date getEndTime(int year, int month) {
        YearMonth yearMonth = YearMonth.of(year, month);
        LocalDate endOfMonth = yearMonth.atEndOfMonth();
        LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
        return Date.from(zonedDateTime.toInstant());
    }

收藏一个大神返回值的枚举类型写法

package com.包路径.dto;

public enum ResultEnums {
    TRUE_200(200, "成功"),
    FALSE_300(300, "失败");

    private int code;
    private String message;

    ResultEnums(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}

使用@Autowired注解时被标红线,找不到bean

解决:Could not autowire. No beans of 'BaseNoticeMapper' type found.

在这里插入图片描述 在用idea写一个实现类时引用了mapper类的来调用dao层的处理,使用@Autowired注解时被标红线,找不到bean。

决办法:在mapper加@mapper或者@repository注解。

这两种注解的区别在于:

@Mapper 加在 Dao层后,service类 @autowired idea会报红,提示找不到bean,但是不影响效果; @Repository 加在 Dao层后,需要在SpringBootApplication 类

在这里插入图片描述 在这里插入图片描述

加@MapperScan表明数据接口层包路径。 idea不会提示报红;