How about:
LocalDate endOfMonth = date.dayOfMonth().withMaximumValue();
dayOfMonth()
returns a LocalDate.Property
which represents the "day of month" field in a way which knows the originating LocalDate
.
As it happens, the withMaximumValue()
method is even documented to recommend it for this particular task:
This operation is useful for obtaining a LocalDate on the last day of the month, as month lengths vary.
LocalDate lastDayOfMonth = dt.dayOfMonth().withMaximumValue();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…