如果你只需要获取当前日期往前推30天的那个具体日期,而不需列表形式,可以简化代码如下:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateBeforeThirtyDays {
public static void main(String[] args) {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 往前推30天
LocalDate thirtyDaysBefore = currentDate.minusDays(30);
// 格式化日期输出(可选)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = thirtyDaysBefore.format(formatter);
System.out.println("当前日期往前推30天的日期是: " + formattedDate);
}
}
这段代码首先获取当前的日期,然后使用minusDays
方法减去30天得到30天前的日期。最后,虽然可选,但示例中还包括了如何使用DateTimeFormatter
来格式化日期的显示方式,使其以“yyyy-MM-dd”的格式输出。