get Month Start LocalDate - Java java.time

Java examples for java.time:LocalDate

Description

get Month Start LocalDate

Demo Code


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

public class Main {

    public static LocalDate getMonthStartLocalDate(
            LocalDate targetLocalDate, int baseDate) {
        LocalDate startLocalDate = targetLocalDate.withDayOfMonth(baseDate);
        return getStartLocalDate(targetLocalDate, startLocalDate, baseDate,
                ChronoUnit.MONTHS);
    }/*from   ww  w  .  j ava2s .c  o  m*/

    private static LocalDate getStartLocalDate(LocalDate targetLocalDate,
            LocalDate startLocalDate, int baseDate, ChronoUnit chronoUnit) {
        if (targetLocalDate.getMonth() == startLocalDate.getMonth()
                && targetLocalDate.getDayOfMonth() < baseDate) {
            startLocalDate = startLocalDate.minus(1, chronoUnit);
        }
        return startLocalDate;
    }
}

Related Tutorials