java获取两个时间之间的所有日期、月份、年份,返回列表

 需求描述

  • 输入开始时间和结束时间,获取时间范围内的所有日期,月份、年份

    (输入可以为"yyyy-MM-dd HH:mm:ss"或者"yyyy-MM-dd")

一、输入开始时间和结束时间,返回时间范围内中的所有日期列表 

/**
     * 传入两个时间范围,返回这两个时间范围内的所有日期,并保存在一个集合中
     *
     * @param beginTime
     * @param endTime
     * @return
     * @throws Exception
     */
    public static List<String> findEveryDay(String beginTime, String endTime)
            throws Exception {
        //创建一个放所有日期的集合
        List<String> dates = new ArrayList();
 
        //创建时间解析对象规定解析格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 
        //将传入的时间解析成Date类型,相当于格式化
        Date dBegin = sdf.parse(beginTime);
        Date dEnd = sdf.parse(endTime);
 
        //将格式化后的第一天添加进集合
        dates.add(sdf.format(dBegin));
 
        //使用本地的时区和区域获取日历
        Calendar calBegin = Calendar.getInstance();
 
        //传入起始时间将此日历设置为起始日历
        calBegin.setTime(dBegin);
 
        //判断结束日期前一天是否在起始日历的日期之后
        while (dEnd.after(calBegin.getTime())) {
 
            //根据日历的规则:月份中的每一天,为起始日历加一天
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
 
            //得到的每一天就添加进集合
            dates.add(sdf.format(calBegin.getTime()));
            //如果当前的起始日历超过结束日期后,就结束循环
        }
        return dates;
    }

测试输出结果:

二、输入开始时间和结束时间,返回时间范围内中的所有月份列表

/**
     * 获取两个日期之间的所有月份 (年月)
     *
     * @param startTime
     * @param endTime
     * @return:list
     */
    public static List<String> getMonthBetweenDate(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        // 声明保存日期集合
        List<String> list = new ArrayList<>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
 
            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {
 
                // 把日期添加到集合
                list.add(sdf.format(startDate));
 
                // 设置日期
                calendar.setTime(startDate);
 
                //把月数增加 1
                calendar.add(Calendar.MONTH, 1);
 
                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

 输入:"2022-07-01  00:00:00 "  "2022-10-27  23:59:59 "

返回结果:[2022-07,2022-08,2022-09,2022-10] 

三、输入开始时间和结束时间,返回时间范围内中的所有年份列表

/**
     * 获取两个日期之间的所有年
     *
     * @param startTime
     * @param endTime
     * @return:list
     */
    public static List<String> getYearBetweenDate(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        // 声明保存日期集合
        List<String> list = new ArrayList<>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
 
            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {
 
                // 把日期添加到集合
                list.add(sdf.format(startDate));
 
                // 设置日期
                calendar.setTime(startDate);
 
                //把年数增加 1
                calendar.add(Calendar.YEAR, 1);
 
                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

 输入:"2020-07-01  00:00:00 "  "2022-10-27  23:59:59 "

返回结果:[2020,2021,2022] 

五、计算2个日期之间相差的  以年、月、日为单位,各自计算结果是多少

/**
     * 计算2个日期之间相差的  以年、月、日为单位,各自计算结果是多少
     * 比如:2011-02-02 到  2017-03-02
     *  以年为单位相差为:6年
     *  以月为单位相差为:73个月
     *  以日为单位相差为:2220天
     * @param fromDate
     * @param toDate
     * @return
     */
    public static JSONObject dayCompare(Date fromDate,Date toDate){
        Calendar from = Calendar.getInstance();
        from.setTime(fromDate);
        Calendar to = Calendar.getInstance();
        to.setTime(toDate);
        //只要年月
        int fromYear = from.get(Calendar.YEAR);
        int fromMonth = from.get(Calendar.MONTH);
        int toYear = to.get(Calendar.YEAR);
        int toMonth = to.get(Calendar.MONTH);
        int year = toYear - fromYear;
        int month = toYear * 12 + toMonth - (fromYear * 12  +  fromMonth);
        int day = (int) ((to.getTimeInMillis() - from.getTimeInMillis()) / (24  *  3600  *  1000));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("year",year);
        jsonObject.put("month",month);
        jsonObject.put("day",day);
        log.info(jsonObject.toJSONString());
        return jsonObject;
    }

    

六、在指定的时间上增加指定月数获取到一个新日期 

/**
     * 在指定的时间上增加指定月数获取到一个新日期
     * @param sourceDate 指定时间
     * @param month 指定增加的月数
     * @return
     */
    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);

        return c.getTime();
    }

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. after(Date when) - 比较两个日期判断当前日期是否在when日期之后 2. before(Date when) - 比较两个日期判断当前日期是否在when日期之前 3. clone() - 克隆当前日期对象 4. compareTo(Date anotherDate) - 比较两个日期返回值为0表示相等,小于0表示当前日期在anotherDate日期之前,大于0表示当前日期在anotherDate日期之后 5. equals(Object obj) - 判断当前日期对象是否与另一个日期对象相等 6. getTime() - 返回当前日期对象表示的时间戳,以毫秒为单位 7. hashCode() - 返回当前日期对象的哈希值 8. setTime(long time) - 设置当前日期对象表示的时间戳 9. toString() - 将当前日期对象转换为字符串形式 10. toInstant() - 将当前日期对象转换为Instant对象 11. toLocaleString() - 将当前日期对象转换为本地化字符串形式 12. toGMTString() - 将当前日期对象转换为GMT标准时间字符串形式 13. toInstant() - 将当前日期对象转换为Instant对象 14. setTimezoneOffset(int offset) - 设置当前日期对象的时区偏移量 15. from(Instant instant) - 将Instant对象转换为日期对象 16. from(ZonedDateTime zdt) - 将ZonedDateTime对象转换为日期对象 17. getYear() - 返回当前日期对象的年份(从1900年开始计算) 18. getMonth() - 返回当前日期对象的月份(0表示1月,11表示12月) 19. getDate() - 返回当前日期对象的日份 20. getDay() - 返回当前日期对象的星期几(0表示星期日,6表示星期六) 21. getHours() - 返回当前日期对象的小时数(24小时制) 22. getMinutes() - 返回当前日期对象的分钟数 23. getSeconds() - 返回当前日期对象的秒数 24. getTimezoneOffset() - 返回当前日期对象的时区偏移量 25. setYear(int year) - 设置当前日期对象的年份 26. setMonth(int month) - 设置当前日期对象的月份 27. setDate(int date) - 设置当前日期对象的日份 28. setHours(int hours) - 设置当前日期对象的小时数 29. setMinutes(int minutes) - 设置当前日期对象的分钟数 30. setSeconds(int seconds) - 设置当前日期对象的秒数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值