分别获取所选期间,所选期间上月,所选期间上年,所选期间上年上月。
代码块
代码块语法遵循标准markdown代码,例如:
//arrayDate = 201801M date = 所选日期编码,ps:selectDate
public String getEndDate(String arrayDate, String date) {
String endDate = null;
//日期格式格式化
SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
Date parse;
try {
String prefix = arrayDate.substring(0, arrayDate.length()-1);//获取可运算时间字符串,原格式为201801M
String suffix = arrayDate.substring(arrayDate.length()-1);// M
parse = format.parse(prefix);
Calendar c = Calendar.getInstance();
c.setTime(parse);
if("selectDate".equals(date)) {//所选期间
endDate = arrayDate;
}else if("selectLastYear".equals(date)){//所选期间上年
c.add(Calendar.YEAR, -1);
Date y = c.getTime();
endDate = format.format(y) + suffix;//结果值加上字母M
}else if("selectLastMonth".equals(date)){//所选期间上月
c.add(Calendar.MONTH, -1);
Date m = c.getTime();
endDate = format.format(m) + suffix;//结果值加上字母M
}else if ("selectLastYearLastMonth".equals(date)) {//所选期间上年上月
c.add(Calendar.MONTH, -13);
Date m = c.getTime();
endDate = format.format(m) + suffix;//结果值加上字母M
}else {
endDate = date;
}
System.out.println("date:"+endDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return endDate;
}