Java获取N天前,N天后的日期(如3天)

熊孩纸 阅读:2407 2020-07-17 13:46:11 评论:0

3天前:取负值 

  Calendar calendar1 = Calendar.getInstance();
  SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
  calendar1.add(Calendar.DATE, -3);
  String three_days_ago = sdf1.format(calendar1.getTime());
  System.out.println(three_days_ago);
  

同理,3天后,取正值即可:
  Calendar calendar2 = Calendar.getInstance();
  SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
  calendar2.add(Calendar.DATE, 3);
  String three_days_after = sdf2.format(calendar2.getTime());
  System.out.println(three_days_after);

标签:java日期
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号