package com.web.controller;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
public class TestDate {
public static String getLast12Months(int i) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MONTH, -i);
Date m = c.getTime();
return sdf.format(m);
}
public static void main(String[] args) throws Exception {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -4);
StringBuilder sb = new StringBuilder();
sb.append(c.get(Calendar.YEAR)).append(c.get(Calendar.MONTH));
String before_six = sb.toString();
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");// 格式化为年月
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(before_six));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(sdf.format(new Date())));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
System.out.println(result);
//倒序
Collections.reverse(result);
System.out.println(result);
}
}
java 计算前6个月月份
猜你喜欢
转载自blog.csdn.net/xinyu100100/article/details/86546907
今日推荐
周排行