美文网首页JavaWeb 知识点
java 中 使用 jodatime 获取一段时间内的星期集合

java 中 使用 jodatime 获取一段时间内的星期集合

作者: _Rondo | 来源:发表于2019-02-14 15:42 被阅读181次

    一、应用场景

    公司做统计相关业务总会用到一些和时间相关处理,java7的canlendar用着实在太难受了,java8的我没有研究过,我不知道前端处理日历是否简单,我这边都是处理好返回前台,直接调用就好了

    二、实例

    1、引入jodatime

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.9</version>
    </dependency>       
    

    2、controller

    /***
     * 日期controller
     * @author wx
     * @Date 2019-02-14
     */
    @RestController
    @RequestMapping("/api/datetime")
    public class DateTimeController {
    
    
        /**
         * 获取一段时间内的星期集合
         * @param startDate
         * @param endDate
         * @param req
         * @param resp
         * @return
         */
        @GetMapping("/getWeekListByDates")
        public BaseRetBean getWeekListByDates(@RequestParam(value = "startDate",required = true)String startDate,
                                              @RequestParam(value = "endDate",required = true)String endDate,
                                              HttpServletRequest req, HttpServletResponse resp){
            BaseRetBean baseRetBean = new BaseRetBean();
            baseRetBean.setRet(1);
            baseRetBean.setMsg("成功");
            baseRetBean.setRows(this.converWeekList(new LocalDate(startDate),new LocalDate(endDate)));
            return baseRetBean;
        }
    
        /**
         * 获取本月星期集合
         * @param req
         * @param resp
         * @return
         */
        @GetMapping("/getLocalMonthWeekList")
        public BaseRetBean getLocalMonthWeekList(HttpServletRequest req, HttpServletResponse resp){
            BaseRetBean baseRetBean = new BaseRetBean();
            baseRetBean.setRet(1);
            baseRetBean.setMsg("成功");
    
            LocalDate now = LocalDate.now();
            LocalDate endDate = now.dayOfMonth().withMaximumValue();
            LocalDate startDate = now.dayOfMonth().withMinimumValue();
            baseRetBean.setRows(this.converWeekList(startDate,endDate));
            return baseRetBean;
        }
    
        /**
         * 转换weeklist
         * @param startDate
         * @param endDate
         * @return
         */
        private List<String[]> converWeekList(LocalDate startDate,LocalDate endDate){
    
            List<String[]> weekList = new ArrayList<>();
            //转换成joda-time的对象
            LocalDate firstDay = startDate.dayOfWeek().withMinimumValue();
            LocalDate lastDay = endDate.dayOfWeek().withMaximumValue();
            //计算两日期间的区间天数
            Period p = new Period(firstDay, lastDay, PeriodType.days());
            int days = p.getDays();
            if (days > 0){
                int weekLength = 7;
                for(int i=0;i<days;i=i+weekLength){
                    String monDay = firstDay.plusDays(i).toString("yyyy-MM-dd");
                    String sunDay = firstDay.plusDays(i+6).toString("yyyy-MM-dd");
                    String [] week = {monDay,sunDay};
                    weekList.add(week);
                }
            }
            return weekList;
        }
    }
    
    

    参考文章:
    https://blog.csdn.net/x3455/article/details/80593319
    https://www.jianshu.com/p/0459c71930df
    https://www.jianshu.com/p/efdeda608780
    -end-

    相关文章

      网友评论

        本文标题:java 中 使用 jodatime 获取一段时间内的星期集合

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