Get first and last day of month using threeten, LocalDate

后端 未结 10 2477
鱼传尺愫
鱼传尺愫 2020-12-07 14:06

I have a LocalDate which needs to get the first and last day of the month. How do I do that?

eg. 13/2/2014 I need to get 1/2/2014 and

10条回答
  •  再見小時候
    2020-12-07 15:01

    You can try this to avoid indicating custom date and if there is need to display start and end dates of current month:

        LocalDate start = LocalDate.now().minusDays(LocalDate.now().getDayOfMonth()-1);
        LocalDate end = LocalDate.now().minusDays(LocalDate.now().getDayOfMonth()).plusMonths(1);
        System.out.println("Start of month: " + start);
        System.out.println("End of month: " + end);
    

    Result:

    >     Start of month: 2019-12-01
    >     End of month: 2019-12-30
    

提交回复
热议问题