获取某一天的前多少天多少月多少年的时间

时间:2023-02-19 21:26:54
 1     import java.util.Calendar;  
 2     public class TestGetLastMonth {  
 3             /** 
 4              * 获取指定月的前一月(年)或后一月(年) 
 5              * @param dateStr 
 6              * @param addYear 
 7              * @param addMonth 
 8              * @param addDate 
 9              * @return 输入的时期格式为yyyy-MM,输出的日期格式为yyyy-MM 
10              * @throws Exception 
11              */  
12             public static String getLastMonth(String dateStr,int addYear, int addMonth, int addDate) throws Exception {  
13                   try {  
14                   java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM");  
15                   java.util.Date sourceDate = sdf.parse(dateStr);  
16                   Calendar cal = Calendar.getInstance();  
17                   cal.setTime(sourceDate);  
18                   cal.add(Calendar.YEAR,addYear);  
19                   cal.add(Calendar.MONTH, addMonth);  
20                   cal.add(Calendar.DATE, addDate);  
21       
22                   java.text.SimpleDateFormat returnSdf = new java.text.SimpleDateFormat("yyyy-MM");  
23                   String dateTmp = returnSdf.format(cal.getTime());  
24                   java.util.Date returnDate = returnSdf.parse(dateTmp);  
25                   return dateTmp;  
26                   } catch (Exception e) {  
27                   e.printStackTrace();  
28                   throw new Exception(e.getMessage());  
29                   }  
30                   }   
31               
32             /** 
33              * 获取指定月的前一月(年)或后一月(年) 
34              * @param dateStr 
35              * @param addYear 
36              * @param addMonth 
37              * @param addDate 
38              * @return 输入的时期格式为yyyy-MM-dd,输出的日期格式为yyyy-MM-dd 
39              * @throws Exception 
40              */  
41             public static String getLastDay(String dateStr,int addYear, int addMonth, int addDate) throws Exception {  
42                   try {  
43                   java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");  
44                   java.util.Date sourceDate = sdf.parse(dateStr);  
45                   Calendar cal = Calendar.getInstance();  
46                   cal.setTime(sourceDate);  
47                   cal.add(Calendar.YEAR,addYear);  
48                   cal.add(Calendar.MONTH, addMonth);  
49                   cal.add(Calendar.DATE, addDate);  
50       
51                   java.text.SimpleDateFormat returnSdf = new java.text.SimpleDateFormat("yyyy-MM-dd");  
52                   String dateTmp = returnSdf.format(cal.getTime());  
53                   java.util.Date returnDate = returnSdf.parse(dateTmp);  
54                   return dateTmp;  
55                   } catch (Exception e) {  
56                   e.printStackTrace();  
57                   throw new Exception(e.getMessage());  
58                   }  
59                   }   
60               
61             /** 
62              * 测试 
63              * @param args 
64              */  
65             public static void main(String[] args) {  
66                   try{  
67                      System.out.println(TestGetLastMonth.getLastMonth("2011-06",0,-1,0));//2011-05  
68                      System.out.println(TestGetLastMonth.getLastMonth("2011-06",0,-6,0));//2010-12  
69                      System.out.println(TestGetLastMonth.getLastMonth("2011-06",-1,0,0));//2010-06  
70                   }catch (Exception e) {  
71                       
72                 }  
73           
74             }  
75         }