localdate 年月转换,使用Java中的LocalDate翻转月份的日期?

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++;

}

解决方案

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);

Which outputs 2015-06-02

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值