java:获取最近六个月的年月并顺序排序

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Collections;

import java.util.Date;

import java.util.List;

 

public class testDate {

public static void main(String[] args) {

String month = null;

String dateString;

Calendar cal = Calendar.getInstance();

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM”);

Date date = new Date();

dateString = sdf.format(cal.getTime());

List<String> rqList = new ArrayList<>();

System.out.println(“倒序前\n”);

for (int i = 0; i < 6; i++) {

dateString = sdf.format(cal.getTime());

 

System.out.println(“dateString”+dateString);

 

rqList.add(dateString.substring(0, 7));

//xfzeList.add(xfze);

cal.add(Calendar.MONTH, -1);

}

// 倒序

Collections.reverse(rqList);

System.out.println(“倒序后\n”);

for(int i=0;i<rqList.size();i++){

System.out.println(“倒序后日期:”+rqList.get(i));

}

}

}

///输出结果如下:/

倒序前

dateString2017-06
dateString2017-05
dateString2017-04
dateString2017-03
dateString2017-02
dateString2017-01
倒序后

倒序后日期:2017-01
倒序后日期:2017-02
倒序后日期:2017-03
倒序后日期:2017-04
倒序后日期:2017-05
倒序后日期:2017-06

    原文作者:寒江雪江南岸蓑笠翁
    原文地址: https://blog.csdn.net/u013592116/article/details/72967356
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞