Get first and last day of month using threeten, LocalDate

后端 未结 10 2463
鱼传尺愫
鱼传尺愫 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 14:58

    if you want to do it only with the LocalDate-class:

    LocalDate initial = LocalDate.of(2014, 2, 13);
    
    LocalDate start = LocalDate.of(initial.getYear(), initial.getMonthValue(),1);
    
    // Idea: the last day is the same as the first day of next month minus one day.
    LocalDate end = LocalDate.of(initial.getYear(), initial.getMonthValue(), 1).plusMonths(1).minusDays(1);
    

提交回复
热议问题