首页 > 解决方案 > Formatting LocalDate in Java

问题描述

I defined the following format in Java :

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

My application fetches a certain date from the calendar:

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

I want to store an additional two dates in format of dtf. The startOfMonth and endOfMonth of the given localDate.

E.g. if localDate is "2019-12-12" I want to create the following two variables -

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

How can I do that?

标签: javadatetimelocaldate

解决方案


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

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


推荐阅读