Get first and last day of month using threeten, LocalDate

user1746050 picture user1746050 · Mar 6, 2014 · Viewed 98.5k times · Source

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 28/2/2014 in LocalDate formats.

Using threeten LocalDate class.

Answer

Jon Skeet picture Jon Skeet · Mar 6, 2014

Just use withDayOfMonth, and lengthOfMonth():

LocalDate initial = LocalDate.of(2014, 2, 13);
LocalDate start = initial.withDayOfMonth(1);
LocalDate end = initial.withDayOfMonth(initial.lengthOfMonth());