JAVA 输出指定日期的前一个月的每一天

JAVA 输出指定日期的前一个月的每一天


 1         String str = "20150623";      //指定日期
 2         Calendar calendar = Calendar.getInstance();
 3         try {
 4             SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");   //日期格式化
 5             Date date = sdf.parse(str);
 6             calendar.setTime(date);
 7             //calendar.add(calendar.DAY_OF_YEAR, -1);
 8             Date enddate = calendar.getTime();
 9             
10             //System.out.println(enddate.toString());
11             
12             calendar.setTime(date);
13             calendar.add(calendar.MONTH, -1);    //得到当前日期减一个月的时间点
14             //Date begindate = calendar.getTime();
15             
16             while(calendar.getTime().before(enddate)){               
17                 System.out.println(sdf.format(calendar.getTime()));
18                  
19                 calendar.add(Calendar.DAY_OF_MONTH, 1);               
20             }
21             
22             
23             
24         } catch (ParseException e) {
25             System.out.println(e.getMessage());  
26         } 

输出:

20150523
20150524
20150525
20150526
20150527
20150528
20150529
20150530
20150531
20150601
20150602
20150603
20150604
20150605
20150606
20150607
20150608
20150609
20150610
20150611
20150612
20150613
20150614
20150615
20150616
20150617
20150618
20150619
20150620
20150621
20150622

 
原文地址:https://www.cnblogs.com/riyueyuzhuzhu/p/5611560.html