获取当前时间前几个月的时间集合
public static List<String> getData(int data){ ArrayList<String> result = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月 Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.add(Calendar.MONTH, 0); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); min.add(Calendar.MONTH, -data); 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); } return result; }
public static void main(String[] args) { List<String> result = getData(3); for (String a:result){ System.out.println(a); } }