获取过去12个月

 1 /**
 2      * 获取过去的12个月 当dateSource为空时获取的当前月份之前的十二个月,不为空时获取指定月份之前的十二个月
 3      */
 4     public static String[] getPastTwelveYearMonth(String dateSource) {
 5         String[] lastTwelveMonths = new String[12];
 6         Calendar calendar = Calendar.getInstance();
 7         //如果当前日期大于二月份的天数28天或者29天会导致计算月份错误,会多出一个三月份,故设置一个靠前日期解决此问题
 8         calendar.set(Calendar.DAY_OF_MONTH, 1);
 9         if (!FCRMStringUtils.isEmpty(dateSource)) {
10             calendar = setTime(dateSource);
11         }
12         for (int i = 0; i < 12; i++) {
13             if (calendar.get(Calendar.MONTH) != 0) {
14                 if (calendar.get(Calendar.MONTH) > 0 && calendar.get(Calendar.MONTH) < 10) {
15                     lastTwelveMonths[11 - i] = calendar.get(Calendar.YEAR) + "-0" + (calendar.get(Calendar.MONTH));
16                 } else {
17                     lastTwelveMonths[11 - i] = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH));
18                 }
19             } else {
20                 lastTwelveMonths[11 - i] = calendar.get(Calendar.YEAR) - 1 + "-" + 12;
21             }
22             //逐次往前推1个月
23             calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
24         }
25         for (int i = 0; i < lastTwelveMonths.length; i++) {
26             System.out.println(lastTwelveMonths[i]);
27         }
28         return lastTwelveMonths;
29     }
30 
31     /**
32      * 获取过去的3个月 当dateSource为空时获取的当前月份之前的三个月,不为空时获取指定月份之前的十三个月
33      */
34     public static String[] getPastThreeYearMonth(String dateSource) {
35         String[] lastThreeMonths = new String[3];
36         Calendar calendar = Calendar.getInstance();
37         //如果当前日期大于二月份的天数28天或者29天会导致计算月份错误,会多出一个三月份,故设置一个靠前日期解决此问题
38         calendar.set(Calendar.DAY_OF_MONTH, 1);
39         if (!FCRMStringUtils.isEmpty(dateSource)) {
40             calendar = setTime(dateSource);
41         }
42         for (int i = 0; i < 3; i++) {
43             if (calendar.get(Calendar.MONTH) != 0) {
44                 if (calendar.get(Calendar.MONTH) > 0 && calendar.get(Calendar.MONTH) < 10) {
45                     lastThreeMonths[2 - i] = calendar.get(Calendar.YEAR) + "-0" + (calendar.get(Calendar.MONTH));
46                 } else {
47                     lastThreeMonths[2 - i] = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH));
48                 }
49             } else {
50                 lastThreeMonths[2 - i] = calendar.get(Calendar.YEAR) - 1 + "-" + 12;
51             }
52             //逐次往前推1个月
53             calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
54         }
55         for (int i = 0; i < lastThreeMonths.length; i++) {
56             System.out.println(lastThreeMonths[i]);
57         }
58         return lastThreeMonths;
59     }
60 
61     /**
62      * 获取指定月份的Calendar对象
63      *
64      * @param dateSource
65      * @return
66      */
67     private static Calendar setTime(String dateSource) {
68         // 创建 Calendar 对象
69         // 默认是当前日期
70         Calendar calendar = Calendar.getInstance();
71         try {
72             // 对 calendar 设置时间的方法
73             // 设置传入的时间格式
74             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
75             // 指定一个日期
76             Date date = dateFormat.parse(dateSource);
77             // 对 calendar 设置为 date 所定的日期
78             calendar.setTime(date);
79         } catch (Exception e) {
80             e.printStackTrace();
81         }
82         return calendar;
83     }
 1 /**
 2      * 获取当前月份的前一个月
 3      *
 4      * @return
 5      */
 6     public static String getPreviousOneMonthString() {
 7         Calendar calendar = Calendar.getInstance();
 8         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
 9         calendar.setTime(new Date());
10         calendar.add(Calendar.MONTH, -1);
11         Date date = calendar.getTime();
12         String month = dateFormat.format(date);
13         return month;
14     }
15 
16     /**
17      * 获取当前月份的下一个月
18      *
19      * @return
20      */
21     public static String getNextOneMonthString(String StrMonth) {
22         Calendar calendar;
23         if(FCRMStringUtils.isNotEmpty(StrMonth)){
24             calendar =  setTime(StrMonth);
25         }else{
26             calendar = Calendar.getInstance();
27             calendar.setTime(new Date());
28         }
29         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
30         calendar.add(Calendar.MONTH, 1);
31         Date date = calendar.getTime();
32         String month = dateFormat.format(date);
33         return month;
34     }
35 
36     /**
37      * 获取当前月份
38      *
39      * @return
40      */
41     public static String getPresentMonthString() {
42         Calendar calendar = Calendar.getInstance();
43         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
44         calendar.setTime(new Date());
45         Date date = calendar.getTime();
46         String month = dateFormat.format(date);
47         return month;
48     }
原文地址:https://www.cnblogs.com/currystar/p/11716328.html