在 Java 中格式化 LocalDate

Formatting LocalDate in Java

我在Java中定义了以下格式:

//define format of YYYYMMDD
private final DateTimeFormatter dtf = DateTimeFormatter.BASIC_ISO_DATE;

我的应用程序从日历中获取特定日期:

LocalDate localDate = fetchDate(); // fetch date in format "yyyy-mm-dd"

我想以 dtf 的格式存储另外两个日期。给定的 localDate.

startOfMonthendOfMonth

例如如果 localDate"2019-12-12" 我想创建以下两个变量 -

String startOfMonth = "20191201";
String endOfMonth = "20191231";

我该怎么做?

LocalDate atStartOfMonth = localDate.with(TemporalAdjusters.firstDayOfMonth());
LocalDate atEndOfMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());

然后按照您想要的方式格式化这些本地日期(即使用您的 dtf 格式化程序)