美文网首页
java计算时间范围内的日期

java计算时间范围内的日期

作者: 鄙人_阿K | 来源:发表于2021-04-13 22:58 被阅读0次
     /**
         * @title 根据开始时间,结束时间获取期间所有日期yyyy-MM-dd
         * 例:开始时间:2019-05-01 结束时间:2019-05-05
         * @param [stime, etime]
         * @return java.util.List<java.lang.String>
         */
        public static List<String> findDates(String stime, String etime) throws ParseException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            Date dBegin = sdf.parse(stime);
            Date dEnd = sdf.parse(etime);
    
            List<String> allDate = new ArrayList();
            allDate.add(sdf1.format(dBegin));
            Calendar calBegin = Calendar.getInstance();
    // 使用给定的 Date 设置此 Calendar 的时间
            calBegin.setTime(dBegin);
            Calendar calEnd = Calendar.getInstance();
    // 使用给定的 Date 设置此 Calendar 的时间
            calEnd.setTime(dEnd);
    // 测试此日期是否在指定日期之后
            while (dEnd.after(calBegin.getTime())) {
    // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
                calBegin.add(Calendar.DAY_OF_MONTH, 1);
                allDate.add(sdf1.format(calBegin.getTime()));
            }
            return allDate;
        }
    

    相关文章

      网友评论

          本文标题:java计算时间范围内的日期

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