java8新增LocalDateTime类,相比SimpleDateFormat好用很多
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
String startTime = dateTimeFormatter.format(localDateTime.minusMinutes(5));
String endTime = dateTimeFormatter.format(localDateTime);
System.out.println(startTime);
System.out.println(endTime);
输出
2021-09-10 17:05
2021-09-10 17:10