`
wxyfighting
  • 浏览: 191728 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多
Java代码收藏代码
  1. /**
  2. *根据原来的时间(Date)获得相对偏移N月的时间(Date)
  3. *@paramprotoDate原来的时间(java.util.Date)
  4. *@paramdateOffset(向前移正数,向后移负数)
  5. *@return时间(java.util.Date)
  6. */
  7. publicstaticDategetOffsetMonthDate(DateprotoDate,intmonthOffset){
  8. Calendarcal=Calendar.getInstance();
  9. cal.setTime(protoDate);
  10. //cal.set(Calendar.MONTH,cal.get(Calendar.MONTH)-monthOffset);//这种写法是错误的,这种偏移以30天为标准
  11. cal.add(Calendar.MONTH,-monthOffset);//正确写法
  12. System.out.println(cal.get(Calendar.MONTH));
  13. returncal.getTime();
  14. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics