3 回答
TA贡献1853条经验 获得超9个赞
Java(任何)解决方案:
Date currentDate = new Date(); // or any date you set
Calendar c = Calendar.getInstance();
c.setTime(currentDate);
c.add(Calendar.DAY_OF_MONTH, 7);
Date currentDatePlusSevenDays = c.getTime();
Java 8+ 解决方案:
Date currentDate = new Date(); // or any date you set
LocalDateTime localDateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
localDateTime = localDateTime.plusDays(7);
Date currentDatePlusSevenDays = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
带有外部库Joda-Time的 Java(任何)解决方案:
DateTime currentDate = DateTime.now(); // or any date you set
DateTime currentDatePlusSevenDays = currentDate.plusDays(7);
TA贡献1836条经验 获得超13个赞
如果你不使用 Java 8,你可以使用下面的代码
int days = 8;
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, days);
SimpleDateFormat format = new SimpleDateFormat("dd/MM/YYYY");
Date newDate = c.getTime();
System.out.println(dateFormat.format(newDate));
TA贡献1874条经验 获得超12个赞
如果你使用 Java 8,你应该使用 LocalDateTime 类。
它让事情变得简单多了。
LocalDateTime dateRedeemed = LocalDateTime.now(); // 2019-06-24T16:31:16.993
// custom date also possible (year, month, day (of month), hours, minutes, seconds and nanoseconds)
LocalDateTime dateRedeemed = LocalDateTime.of(2019, 06, 24, 13, 55, 36, 123);
// Add 7 days
LocalDateTime newDate = dateRedeemed.plusDays(7);
System.out.println(newDate.toString()); // 2019-07-01T16:31:16.993
添加回答
举报