Compare two dates by Year, Month, Day for two LocalDate value - Java java.util

Java examples for java.util:Month

Description

Compare two dates by Year, Month, Day for two LocalDate value

Demo Code


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

public class Main {
    /**/*from   www . ja  v  a2 s . co m*/
     * Compare two dates by Year, Month, Day
     * 
     * @param type
     *            (=0:Year, =1:Month, =2:Day)
     * @param date1
     * @param date2
     * @return
     */
    public static long getTimeGap(int type, LocalDate date1, LocalDate date2) {
        if (type == 0)
            return ChronoUnit.YEARS.between(date1, date2);
        if (type == 1)
            return ChronoUnit.MONTHS.between(date1, date2);
        if (type == 2)
            return date2.get(ChronoField.DAY_OF_YEAR)
                    - date1.get(ChronoField.DAY_OF_YEAR);
        return 0;
    }
}

Related Tutorials