Get first and last day of month using threeten, LocalDate

后端 未结 10 2472
鱼传尺愫
鱼传尺愫 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:04

    YearMonth

    For completeness, and more elegant in my opinion, see this use of YearMonth class.

    YearMonth month = YearMonth.from(date);
    LocalDate start = month.atDay(1);
    LocalDate end   = month.atEndOfMonth();
    

    For the first & last day of the current month, this becomes:

    LocalDate start = YearMonth.now().atDay(1);
    LocalDate end   = YearMonth.now().atEndOfMonth();
    

提交回复
热议问题