get Year End LocalDate - Java java.time

Java examples for java.time:LocalDate

Description

get Year End LocalDate

Demo Code


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

public class Main {

    public static LocalDate getYearEndLocalDate(LocalDate targetLocalDate,
            int baseDate) {
        LocalDate endLocalDate = targetLocalDate.withMonth(1)
                .withDayOfMonth(baseDate).minusDays(1);
        return getEndLocalDate(targetLocalDate, endLocalDate, baseDate,
                ChronoUnit.YEARS);
    }/*from  www.  ja v  a2 s. c  o  m*/

    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