Get first and last day of month using threeten, LocalDate

Get first and last day of month using threeten, LocalDate

Asked on December 25, 2018 in Java.
Add Comment


  • 5 Answer(s)

    Try to use withDayOfMonth(), and lengthOfMonth() like the code below,

    LocalDate initial = LocalDate.of(2014, 2, 13);
    LocalDate start = initial.withDayOfMonth(1);
    LocalDate end = initial.withDayOfMonth(initial.lengthOfMonth());
    
    Answered on December 25, 2018.
    Add Comment

    This Application Program Interface was created to support a solution that matches to business requirements very closely.

    import static java.time.temporal.TemporalAdjusters.*;
     
    LocalDate initial = LocalDate.of(2014, 2, 13);
    LocalDate start = initial.with(firstDayOfMonth());
    LocalDate end = initial.with(lastDayOfMonth());
    
    Answered on December 25, 2018.
    Add Comment

    Here is the straight forward solution,

    YearMonth month = YearMonth.from(date);
    LocalDate start = month.atDay(1);
    LocalDate end = month.atEndOfMonth();
    
    Answered on December 25, 2018.
    Add Comment

    API was designed to support a solution that matches closely to business requirements

    import static java.time.temporal.TemporalAdjusters.*;
    
    LocalDate initial = LocalDate.of(2014, 2, 13);
    LocalDate start = initial.with(firstDayOfMonth());
    LocalDate end = initial.with(lastDayOfMonth());H
    Answered on January 16, 2019.
    Add Comment

    If anyone comes looking for first day of previous month and last day of previous month:

    public static LocalDate firstDayOfPreviousMonth(LocalDate date) {
            return date.minusMonths(1).withDayOfMonth(1);
        }
    
    
    public static LocalDate lastDayOfPreviousMonth(LocalDate date) {
            return date.withDayOfMonth(1).minusDays(1);
        }
    Answered on January 16, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.