我想做类似的事情:
Date date = new Date(); // current date date = date - 300; // substract 300 days from current date and I want to use this "date"
怎么做?
随着 Java 8 的日期时间 API 更改,使用LocalDate
LocalDate
LocalDate date = LocalDate.now().minusDays(300);
同样,您可以拥有
LocalDate date = someLocalDateInstance.minusDays(300);
有关 java.util.Date <--> java.time.LocalDateTime之间的转换,请参阅https://codingdict.com/questions/225923
Date in = new Date(); LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault()); Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
使用Calendar的add()方法
Calendar
add()
Calendar cal = Calendar.getInstance(); cal.setTime(dateInstance); cal.add(Calendar.DATE, -30); Date dateBefore30Days = cal.getTime();