0
点赞
收藏
分享

微信扫一扫

java时间处理方法2-时间集合

public static List<String> getDayTimes(String startTime, String endTime, Integer days) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = sdf.parse(startTime + " 00:00:00");
            Date end = sdf.parse(endTime + " 00:00:00");
            List<String> lDate = new ArrayList<String>();
            //            lDate.add(startTime);
            Calendar calBegin = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calBegin.setTime(start);
            Calendar calEnd = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calEnd.setTime(end);
            // 测试此日期是否在指定日期之后
            while (end.after(calBegin.getTime())) {
                lDate.add(sdf1.format(calBegin.getTime()));
                // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
                if (days < 30) {
                    calBegin.add(Calendar.DAY_OF_MONTH, days);
                } else {
                    calBegin.add(Calendar.MONTH, 1);
                }
            }
            return lDate;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

当days小于30天时,返回每天的时间集合;

当days大于30天时,返回每个月中的一天;

举报

相关推荐

0 条评论