获取当前月份到六个月之前的月份列表,总共七个月

来源:互联网 发布:淘宝v6是最高等级吗 编辑:程序博客网 时间:2024/06/02 16:17
public static void main(String[] args) throws ParseException {Calendar c = Calendar.getInstance();c.add(Calendar.MONTH, -5);String before_six=c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH);//六个月前ArrayList<String> result = new ArrayList<String>();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");// 格式化为年月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);}

某个功能中要用到,留着作为参考
阅读全文
0 0
原创粉丝点击