Roll over the month of dates using LocalDate in Java?(在 Java 中使用 LocalDate 翻转日期的月份?)
问题描述
我正在用 java 设计一个预订系统,它只需要处理 2015 年的日期并使用 LocalDate 类,预订由开始日期和持续时间组成,例如2015 年 5 月 26 日:7 是从 2015 年 5 月 26 日开始的预订,为期 7 天,{26,27,28,29,30,31} 五月和 {1}st Jun.如果我说生成无论如何,我的带有循环的日期是否可以得到正确的月份翻转?所以日期不会说是 5 月 32 日,而是 6 月 1 日.
I am designing a booking system in java that only has to handle dates for the year 2015 and using the LocalDate class, a booking consists of a start date and a duration e.g. 2015, 5, 26: 7 would be a booking starting on 26 may 2015 for a duration of 7 days, {26,27,28,29,30,31} May and the {1}st Jun. If i am say generating my dates with a loop is there anyway to get the correct roll over of the month?, so the date won't say be 32nd of May but instead 1st Jun.
int initialDate=26;
int initialMonth=5;
int duration= 7;
int endDate= initialDate+duration;
LocalDate date;
while(initialDate<=endDate){
date=LocalDate.of(2015, initialMonth, initialDate);
System.out.println(date.getDayOfMonth());
initialDate++;
}
推荐答案
假设您使用的是 Java 8,为什么不使用 LocalDate#plusDays
?
Assuming you're using Java 8, why not use LocalDate#plusDays
?
LocalDate startDate = LocalDate.of(2015, 5, 26);
LocalDate endDate = startDate.plusDays(7);
System.out.println(endDate);
哪个输出2015-06-02
这篇关于在 Java 中使用 LocalDate 翻转日期的月份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中使用 LocalDate 翻转日期的月份?
基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01