zoukankan      html  css  js  c++  java
  • Java获取2个日期里面的所有月份

      public static void main(String[] args) {
            
            String t1="2018-08-01";
            t1 = t1.replaceAll("(\d{4})\-(\d{2})\-\d{2}", "$1-$2-01");
            //String t2="2024-11-30";    
            //String t2="2018-01-30";
            //String t2="2019-09-30";
            String t2 = "2019-09-31";
            t2 = t2.replaceAll("(\d{4})\-(\d{2})\-\d{2}", "$1-$2-01");
    
            List<String> monthArea = getMonthArea(t1,t2);
            
            for(String t : monthArea) {
                System.out.println(t);
            }
            
        }
        private static List<String> getMonthArea(String t1,String t2) {
            
            LocalDate startDate = LocalDate.parse(t1);
            LocalDate endDate = LocalDate.parse(t2);
            
            List<String> monthArea = new ArrayList<>();
            
            if (startDate.compareTo(endDate)>0) {
                return monthArea;        
            }
            
            int startYear = startDate.getYear();
            int startMonth = startDate.getMonthValue();
            
            int endYear = endDate.getYear();
            int endMonth = endDate.getMonthValue();
                    
            if (startYear==endYear) {
                for(int i=startMonth;i<=endMonth;i++) {                
                    monthArea.add(startYear+"-"+getMyMonth(i));
                }                
            }else {        
                for(int i=startYear;i<=endYear;i++) {
                    if (i == startYear ) {
                        calcStartYear(monthArea,startYear,startMonth);                
                    }else if (i == endYear){
                        calcEndYear(monthArea,endYear,endMonth);
                    }else {
                        calcOneYear(monthArea,i);
                    }
                }            
            }
            
            return monthArea;
            
        }
        private static void calcStartYear(List<String> monthArea,int startYear,int startMonth) {
            for(int i=startMonth;i<=12;i++) {                
                monthArea.add(startYear+"-"+getMyMonth(i));            
            }
        }
        private static void calcOneYear(List<String> monthArea,int year) {
            for(int i=1;i<=12;i++) {
                monthArea.add(year+"-"+getMyMonth(i)); 
            }        
        }
        private static void calcEndYear(List<String> monthArea,int endYear,int endMonth) {
            for(int i=1;i<=endMonth;i++) {        
                monthArea.add(endYear+"-"+getMyMonth(i));        
            }
        }    
        private static String getMyMonth(int month){
            String result = "";
            if (month<10) {
                result = "0" + Integer.toString(month);
            }else {
                result = Integer.toString(month);
            }
            return result;
        }
  • 相关阅读:
    PHP 中的魔法常数
    Mac上tesseract-OCR的安装配置
    Java设计模式系列之迭代器模式
    Java设计模式系列之策略模式
    设计模式
    Java设计模式系列之单例模式
    SQl Server 中登录名 、用户、角色、概念一览
    数据结构知识点总结
    面试题收集——Java基础部分(一)
    XML学习总结(一)——XML介绍
  • 原文地址:https://www.cnblogs.com/liw66/p/10209294.html
Copyright © 2011-2022 走看看