zoukankan      html  css  js  c++  java
  • 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;
            }
        }

  • 相关阅读:
    智联招聘
    我的Linux以及软件配置(长期更新)
    关于Git的笔记
    PHP和HTML表单
    web学习笔记——CSS整理(一)
    新开通博客园
    Thinphp模板替换
    __APP__
    大步前行
    centos 7 添加环境变量
  • 原文地址:https://www.cnblogs.com/renpei/p/9645874.html
Copyright © 2011-2022 走看看