java获取指定年月往前推12个月的年月_Java 获取指定日期范围内的每个月,每季度,每一年...

时间:2025-02-16 11:11:56

/**

*根据时间范围获得月份集

* @return

*/

public static List getRangeSet(String beginDate,String endDate){

/*      (Date2),当Date1大于Date2时,返回TRUE,当小于等于时,返回false;

(Date2),当Date1小于Date2时,返回TRUE,当大于等于时,返回false;

如果业务数据存在相等的时候,而且相等时也需要做相应的业务判断或处理时,你需要使用:!(Date2);*/

List rangeSet =null;

SimpleDateFormat sdf = null;

Date begin_date = null;

Date end_date = null;

rangeSet = new ();

sdf = new SimpleDateFormat("yyyy-MM");

try {

begin_date = (beginDate);//定义起始日期

end_date = (endDate);//定义结束日期

} catch (ParseException e) {

("时间转化异常,请检查你的时间格式是否为yyyy-MM或yyyy-MM-dd");

}

Calendar dd = ();//定义日期实例

(begin_date);//设置日期起始时间

while(!().after(end_date)){//判断是否到结束日期

((()));

(, 1);//进行当前日期月份加1

}

return rangeSet;

}

/**

*根据时间范围获得季度集

* @return

*/

public static List getRangeSet_Q(String beginDate,String endDate){

/*      (Date2),当Date1大于Date2时,返回TRUE,当小于等于时,返回false;

(Date2),当Date1小于Date2时,返回TRUE,当大于等于时,返回false;

如果业务数据存在相等的时候,而且相等时也需要做相应的业务判断或处理时,你需要使用:!(Date2);*/

List rangeSet =null;

SimpleDateFormat sdf = null;

Date begin_date = null;

Date end_date = null;

String[] numStr =null;

String Q=null;

rangeSet = new ();

sdf = new SimpleDateFormat("yyyy-MM");

try {

begin_date = (beginDate);//定义起始日期

end_date = (endDate);//定义结束日期

} catch (ParseException e) {

("时间转化异常,请检查你的时间格式是否为yyyy-MM或yyyy-MM-dd");

}

Calendar dd = ();//定义日期实例

(begin_date);//设置日期起始时间

while(!().after(end_date)){//判断是否到结束日期

numStr=  (()).split("-",0);

Q = getQuarter((numStr[1]))+"";

(numStr[0].toString()+"年"+numStr[1].toString()+"月"+"为"+numStr[0].toString()+"年第"+Q+"季");

(Q);

(, 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;

}

}