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

来源:互联网 发布:8月份信贷数据 编辑:程序博客网 时间:2024/05/17 01:15

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);

原创粉丝点击