java 获得相对偏移 N 月的时间

Java代码   收藏代码
  1. /** 
  2.      * 根据原来的时间(Date)获得相对偏移 N 月的时间(Date) 
  3.  
  4.      * @param protoDate 原来的时间(java.util.Date) 
  5.  
  6.      * @param dateOffset(向前移正数,向后移负数) 
  7.  
  8.      * @return 时间(java.util.Date) 
  9.  
  10.      */  
  11.     public static Date getOffsetMonthDate(Date protoDate,int monthOffset){  
  12.         Calendar cal = Calendar.getInstance();  
  13.         cal.setTime(protoDate);  
  14. //      cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - monthOffset);  //这种写法是错误的,这种偏移以30天为标准  
  15.         cal.add(Calendar.MONTH, -monthOffset); //正确写法  
  16.         System.out.println(cal.get(Calendar.MONTH));  
  17.         return cal.getTime();  
  18.     }  
 

你可能感兴趣的:(java,Date,时间)