美文网首页
java查询指定日期前几个月

java查询指定日期前几个月

作者: Geroge1226 | 来源:发表于2020-04-28 14:16 被阅读0次

    1、业务背景

    App开发遇到时间轴,一般由后端返回(原因:放在APP端获取时间会随着手机时间调整而变动),现要求返回当前月前n个月及后m个月的月份(包括页面英文简写)。

    2、DateUtils方法

     public static final String DATE_YYYY_MM = "yyyy-MM";
    /**
         * @name: getMonths
         * @description: 获取指定的月份信息
         * @param offset 偏移量
         * @return: java.lang.String
         * @date: 2020-04-28 13:19
         * @auther: Geroge1226
         *
        */
        public static String getMonths(Date date,int offset) {
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_YYYY_MM);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.MONTH, +offset);
            Date m = c.getTime();
            return sdf.format(m);
        }
    

    3、MonthMapperEnum枚举类

    
    /**
     * All rights Reserved, Designed By www.pousheng.com
     *
     * @version V1.0
     * @Title: MonthMapperEnum
     * @Package com.pousheng.enums
     * @Description: 月份枚举类
     * @author: Geroge1226
     * @date: 2020-04-27 18:00
     * @Copyright: 2019 www.pousheng.cn Inc. All rights reserved.
     * 注意:本内容仅限于XXX内部传阅,禁止外泄以及用于其他的商业目
     */
    public enum  MonthMapperEnum {
    
        JANUARY("01","Jan"),
        FEBRUARY("02","Feb"),
        MARCH("03","Mar"),
        APRIL("04","Apr"),
        MAY("05","May"),
        JUNE("06","Jun"),
        JULY("07","Jul"),
        AUGUST("08","Aug"),
        SEPTEMBER("09","Sep"),
        OCTOBER("10","Oct"),
        NOVEMBER("11","Nov"),
        DECEMBER("12","Dec");
    
        private String month;
        private String shorthand;
    
        MonthMapperEnum(String month, String shorthand) {
            this.month = month;
            this.shorthand = shorthand;
        }
    
        // 根据月份查询对应简写 
        public static String getShortHand(String month){
           for(MonthMapperEnum enu:MonthMapperEnum.values()){
               if(enu.getMonth().equals(month)){
                   return enu.getShorthand();
               }
           }
           return null;
        }
    
        public String getMonth() {
            return month;
        }
    
        public void setMonth(String month) {
            this.month = month;
        }
    
        public String getShorthand() {
            return shorthand;
        }
    
        public void setShorthand(String shorthand) {
            this.shorthand = shorthand;
        }}
    
    

    4、实现层

     public List<MonthInfoDTO> buildBannerMoth(Date date, Integer leftFactor, Integer rigthFator) {
            List<MonthInfoDTO> list = new ArrayList<>();
            // date时间左移量
            for(int i = leftFactor ;i<0;i++){
                String month = DateUtils.getMonths(date,i);
                MonthInfoDTO monthInfoDTO = wrapMonthInfo(month,i);
                list.add(monthInfoDTO);
            }
            // date时间右移量
            for(int j = 0; j<=rigthFator ; j++) {
                String month = DateUtils.getMonths(date,j);
                MonthInfoDTO monthInfoDTO = wrapMonthInfo(month,j);
                list.add(monthInfoDTO);
            }
            System.out.println(list);
            return list;
        }
     
    
     public MonthInfoDTO wrapMonthInfo(String month,Integer offet){
            MonthInfoDTO monthInfoDTO =  new MonthInfoDTO();
            monthInfoDTO.setShortand(MonthMapperEnum.getShortHand(month.split("-")[1]));
            monthInfoDTO.setMonth(month);
            monthInfoDTO.setOffset(offet);
            return monthInfoDTO;
        }
    

    5、实体类

    @Data
    @ApiModel(description = "月份")
    public class MonthInfoDTO {
    
        @ApiModelProperty(value = "月份:yyyy-MM")
        private String month;
    
        @ApiModelProperty(value = "偏移量,-5...0...5排序")
        private Integer offset;
    
        @ApiModelProperty(value = "英文月份简称")
        private String shortand;
    
    }
    

    :以上关键方法参考网络,本次做实践记录,非优雅实现。

    6、返回示例

    
    {
      "code": "000000",
      "message": "Success",
      "result": [
        {
          "month": "2019-11",
          "offset": -5,
          "shortand": "Nov"
        },
        {
          "month": "2019-12",
          "offset": -4,
          "shortand": "Dec"
        },
        {
          "month": "2020-01",
          "offset": -3,
          "shortand": "Jan"
        },
        {
          "month": "2020-02",
          "offset": -2,
          "shortand": "Feb"
        },
        {
          "month": "2020-03",
          "offset": -1,
          "shortand": "Mar"
        },
        {
          "month": "2020-04",
          "offset": 0,
          "shortand": "Apr"
        },
        {
          "month": "2020-05",
          "offset": 1,
          "shortand": "May"
        },
        {
          "month": "2020-06",
          "offset": 2,
          "shortand": "Jun"
        },
        {
          "month": "2020-07",
          "offset": 3,
          "shortand": "Jul"
        },
        {
          "month": "2020-08",
          "offset": 4,
          "shortand": "Aug"
        },
        {
          "month": "2020-09",
          "offset": 5,
          "shortand": "Sep"
        }
      ]
    }
    

    相关文章

      网友评论

          本文标题:java查询指定日期前几个月

          本文链接:https://www.haomeiwen.com/subject/enpvwhtx.html