get Month End LocalDate - Java java.time

Java examples for java.time:LocalDate

Description

get Month End LocalDate

Demo Code


//package com.java2s;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class Main {

    public static LocalDate getMonthEndLocalDate(LocalDate targetLocalDate,
            int baseDate) {
        LocalDate endLocalDate = targetLocalDate.withDayOfMonth(baseDate)
                .minusDays(1);/*  w w  w.  j  av  a2s . com*/
        return getEndLocalDate(targetLocalDate, endLocalDate, baseDate,
                ChronoUnit.MONTHS);
    }

    private static LocalDate getEndLocalDate(LocalDate targetLocalDate,
            LocalDate endLocalDate, int baseDate, ChronoUnit chronoUnit) {
        if (targetLocalDate.getMonth() == endLocalDate.getMonth()
                && targetLocalDate.getDayOfMonth() >= baseDate) {
            endLocalDate = endLocalDate.plus(1, chronoUnit);
        }
        return endLocalDate;
    }
}

Related Tutorials