zoukankan      html  css  js  c++  java
  • java小工具之获取两个日期月份中的所有月份

    例如:要取2019-01  到  2020-06之间的所有月份

    /**获取两月之间的所有月份*/
        public static List<String> getMonthBetween(String minDate, String maxDate){
            ArrayList<String> result = new ArrayList<String>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
    
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
    
            try{
                min.setTime(sdf.parse(minDate));
                min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
    
                max.setTime(sdf.parse(maxDate));
                max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
            }catch (Exception e){
                throw new SSCException("分解月份出错");//注意:抛出的类型是自定义的
            }
    
            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
            min = null;max = null;curr = null;
            return result;
        }
  • 相关阅读:
    Mysql日志管理
    Mysql 安全和DCL语句
    Mysql DDL语句之视图
    Mysql增删改查(DML、DQL)
    Mysql操作之部分DDL语句
    如何做事情
    temp
    asp.net入门
    希望尽快回忆起来
    需求?
  • 原文地址:https://www.cnblogs.com/spll/p/14267690.html
Copyright © 2011-2022 走看看