java,取30天前的日期

		SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
		String maxDateStr = "2017-02-08";
		String minDateStr = "";
		Calendar calc =Calendar.getInstance();
		try {
			calc.setTime(sdf.parse(maxDateStr));
			calc.add(calc.DATE, -30);
			Date minDate = calc.getTime();
			minDateStr = sdf.format(minDate);
			System.out.println("minDateStr:"+minDateStr);//minDateStr:2017-01-09   正确!!!!
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
//		SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
//		String maxDateStr = "2017-02-08";
//		String minDateStr = "";
//		try {
//			Date minDate = new Date(sdf.parse(maxDateStr).getTime() - (30-1)*24*3600*1000);//minDateStr:2017-02-28   错误的!!!!!!
//			minDateStr = sdf.format(minDate);
//			System.out.println("minDateStr:"+minDateStr);  //"2017-02-06"<= ... <=  "2017-02-08"  //3天
//		} catch (ParseException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}


前30天日期序列

		 SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");  
	        String maxDateStr = "2017-02-08";  
	        String minDateStr = "";  
	        Calendar calc =Calendar.getInstance();  
	        try {  
	            for(int i=0;i<30;i++){
	            	calc.setTime(sdf.parse(maxDateStr));  
		            calc.add(calc.DATE, -i);  
		            Date minDate = calc.getTime();  
		            minDateStr = sdf.format(minDate);  
		            System.out.println(i+":minDateStr:"+minDateStr);//minDateStr:2017-01-09   正确!!!!  
	            }
	        } catch (ParseException e1) {  
	            // TODO Auto-generated catch block  
	            e1.printStackTrace();  
	        }
0:minDateStr:2017-02-08
1:minDateStr:2017-02-07
2:minDateStr:2017-02-06
3:minDateStr:2017-02-05
4:minDateStr:2017-02-04
5:minDateStr:2017-02-03
6:minDateStr:2017-02-02
7:minDateStr:2017-02-01
8:minDateStr:2017-01-31
9:minDateStr:2017-01-30
10:minDateStr:2017-01-29
11:minDateStr:2017-01-28
12:minDateStr:2017-01-27
13:minDateStr:2017-01-26
14:minDateStr:2017-01-25
15:minDateStr:2017-01-24
16:minDateStr:2017-01-23
17:minDateStr:2017-01-22
18:minDateStr:2017-01-21
19:minDateStr:2017-01-20
20:minDateStr:2017-01-19
21:minDateStr:2017-01-18
22:minDateStr:2017-01-17
23:minDateStr:2017-01-16
24:minDateStr:2017-01-15
25:minDateStr:2017-01-14
26:minDateStr:2017-01-13
27:minDateStr:2017-01-12
28:minDateStr:2017-01-11
29:minDateStr:2017-01-10

你可能感兴趣的:(Java)