Java 獲取指定日期范圍內的每個月,每季度,每一年


    /**
     *根據時間范圍獲得月份集
     * @return
     */
    public static List<String> getRangeSet(String beginDate,String endDate){
        /*      Date1.after(Date2),當Date1大於Date2時,返回TRUE,當小於等於時,返回false;
          Date1.before(Date2),當Date1小於Date2時,返回TRUE,當大於等於時,返回false;
          如果業務數據存在相等的時候,而且相等時也需要做相應的業務判斷或處理時,你需要使用:!Date1.after(Date2);*/
        List<String> rangeSet =null;
        SimpleDateFormat sdf = null;
          Date begin_date = null;
          Date end_date = null;
          rangeSet = new java.util.ArrayList<String>();
          sdf = new SimpleDateFormat("yyyy-MM");
        try {
              begin_date = sdf.parse(beginDate);//定義起始日期
              end_date = sdf.parse(endDate);//定義結束日期
        } catch (ParseException e) {
            System.out.println("時間轉化異常,請檢查你的時間格式是否為yyyy-MM或yyyy-MM-dd");
        }
          Calendar dd = Calendar.getInstance();//定義日期實例
          dd.setTime(begin_date);//設置日期起始時間
          while(!dd.getTime().after(end_date)){//判斷是否到結束日期
              rangeSet.add(sdf.format(dd.getTime()));
              dd.add(Calendar.MONTH, 1);//進行當前日期月份加1
          }
          return rangeSet;
    }   

 

 

 

    /**
     *根據時間范圍獲得季度集
     * @return
     */
    public static List<String> getRangeSet_Q(String beginDate,String endDate){
        /*      Date1.after(Date2),當Date1大於Date2時,返回TRUE,當小於等於時,返回false;
          Date1.before(Date2),當Date1小於Date2時,返回TRUE,當大於等於時,返回false;
          如果業務數據存在相等的時候,而且相等時也需要做相應的業務判斷或處理時,你需要使用:!Date1.after(Date2);*/
        List<String> rangeSet =null;
        SimpleDateFormat sdf = null;
          Date begin_date = null;
          Date end_date = null;
          String[] numStr =null;
          String Q=null;
          rangeSet = new java.util.ArrayList<String>();
          sdf = new SimpleDateFormat("yyyy-MM");
        try {
              begin_date = sdf.parse(beginDate);//定義起始日期
              end_date = sdf.parse(endDate);//定義結束日期
        } catch (ParseException e) {
            System.out.println("時間轉化異常,請檢查你的時間格式是否為yyyy-MM或yyyy-MM-dd");
        }
          Calendar dd = Calendar.getInstance();//定義日期實例
          dd.setTime(begin_date);//設置日期起始時間
          while(!dd.getTime().after(end_date)){//判斷是否到結束日期
              numStr=  sdf.format(dd.getTime()).split("-",0);
              Q = getQuarter(Integer.valueOf(numStr[1]))+"";
              System.out.println(numStr[0].toString()+"年"+numStr[1].toString()+"月"+"為"+numStr[0].toString()+"年第"+Q+"季");
              rangeSet.add(Q);
              dd.add(Calendar.MONTH, 1);//進行當前日期月份加1
          }
          return rangeSet;
    }   

 

 

 

    /**
     * 根據月獲得季度
     * @param month  月
     * @return  季度
     */
    private static int getQuarter(int month) {
        if(month == 1 || month == 2 || month == 3){
            return 1;
        }else if(month == 4 || month == 5 || month == 6){
            return  2;
        }else if(month == 7 || month == 8 || month == 9){
            return 3;
        }else{
            return 4;
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM