java - 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?
解决方案
LocalDate atStartOfMonth = localDate.with(TemporalAdjusters.firstDayOfMonth());
LocalDate atEndOfMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
然后你以你想要的方式格式化这些本地日期(即使用你的dtf
格式化程序)
推荐阅读
- html - Custom URL Protocol with arguments is looking for the arguments inside Mozilla's directory
- python - Python Discord.py 客户端语法无效
- windows - git: 'ssh-keygen' 不是 git 命令。见'git --help'
- html - 旋转木马渐变黑色效果
- python - python - 如何在空格键上暂停和取消暂停程序
- php - 循环以在 Wordpress 中插入带有 3 张带有偏移量的卡片的新轮播项目
- c# - “X 之一”对一实体关系
- android - Error on Android build: "aapt2: error=2, No such file or directory"
- python - Changing multiple column values to binary values
- windows - 如何确定应用程序使用 OllyDbg 终止的方式?