本年度12个月的获取 java
程序员文章站
2024-01-25 20:31:34
...
本年度12个月的获取
@Test
public void test(){
// 获取当前年份、月份、日期
Calendar cale = null;
cale = Calendar.getInstance();
int month = cale.get(Calendar.MONTH) + 1;
// 获取当月第一天和最后一天
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd”);
String firstday, lastday;
// 获取前月的第一天
for(int i = 0;i<12;i++){
int nnn=0;
if(i>=month){
nnn=i-12;
System.out.println(nnn);
}else{
nnn=i;
}
cale = Calendar.getInstance();
cale.set(Calendar.MONTH, nnn);
cale.set(Calendar.DAY_OF_MONTH, 1);
firstday = format.format(cale.getTime());
// 获取前月的最后一天
cale = Calendar.getInstance();
cale.set(Calendar.MONTH, nnn+1);
cale.set(Calendar.DAY_OF_MONTH, 0);
lastday = format.format(cale.getTime());
Map<String, Object> paramMap = new HashMap();
paramMap.put(“canshu”,"");
paramMap.put(“start”,firstday);
paramMap.put(“end”,lastday);
System.out.println("本月第一天和最后一天分别是 : " + firstday + " and " + lastday);
}
}