使用Java中的LocalDate翻转月份的日期? [英] Roll over the month of dates using LocalDate in Java?

查看:1125
本文介绍了使用Java中的LocalDate翻转月份的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个java预订系统,只需要处理2015年的日期和使用LocalDate类,预订包括开始日期和持续时间。 2015,5,26:7将是从2015年6月26日开始的预订,为期7天,{26,27,28,29,30,31} 5月和{1} st Jun。如果我说我的日期与一个循环是有反正正确的翻滚月,所以日期不会说是五月,但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翻转月份的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆