Get first and last day of month using threeten, LocalDate

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

    Try this:

    LocalDate initial = LocalDate.of(2014, 2, 13);
    LocalDate start = initial.withDayOfMonth(1);         
    LocalDate end = initial.withDayOfMonth(initial.getMonthOfYear().getLastDayOfMonth(false));
    System.out.println(start);
    System.out.println(end);
    

    you can find the desire output but need to take care of parameter true/false for getLastDayOfMonth method

    that parameter denotes leap year

提交回复
热议问题