Java LocalDateTime Calculate areDatesOneMonthApart(LocalDateTime start, LocalDateTime end)

Here you can find the source of areDatesOneMonthApart(LocalDateTime start, LocalDateTime end)

Description

are Dates One Month Apart

License

Open Source License

Declaration

public static boolean areDatesOneMonthApart(LocalDateTime start, LocalDateTime end) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.time.*;

public class Main {
    public static boolean areDatesOneMonthApart(LocalDateTime start, LocalDateTime end) {
        final int startDay = start.getDayOfMonth();
        final int endDay = end.getDayOfMonth();

        if (startDay == endDay && (start.getMonth() != start.getMonth())) {
            return true;
        } else {/*from   w w w .  ja v a2  s. c o m*/
            // could be because one of the two is the last day of the month
            final boolean startAtMonthEnd = (startDay == start.getMonth().length(start.toLocalDate().isLeapYear()));
            final boolean endAtMonthEnd = (endDay == end.getMonth().length(end.toLocalDate().isLeapYear()));
            if (startAtMonthEnd) {
                return endAtMonthEnd || endDay > startDay; // i.e., either both are month ends, or end day is 'usual' day and start day was month-constrained
            } else {
                return endAtMonthEnd && endDay < startDay; // i.e., this is a short month (and since above has evaluated false, can't be both at month end
            }
        }
    }
}

Related

  1. add(final LocalDateTime original, final int years, final int months, final int days, final int hours, final int minutes)
  2. addHeureMinute(final LocalDateTime time, final int heures, final int minutes)
  3. atMidnight(LocalDateTime value)
  4. balanceStartAndEndDateTime(LocalDateTime startDateTime, LocalDateTime endDateTime)
  5. before(LocalDateTime time1, LocalDateTime time2)
  6. calcNextDayOfWeekFromLDT(LocalDateTime ldt, DayOfWeek dow)