Java Date Compare by Year sameYear(Date date1, Date date2)

Here you can find the source of sameYear(Date date1, Date date2)

Description

Checks if the two given date have the same year.

License

Open Source License

Parameter

Parameter Description
date1 a parameter
date2 a parameter

Declaration

public static boolean sameYear(Date date1, Date date2) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**//from w  ww.j a v a 2  s  . c o  m
     * Checks if the two given date have the same year.
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static boolean sameYear(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return false;
        }
        return getYear(date1) == getYear(date2);
    }

    /**
     * Extracts the year value from the given date.
     * 
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        if (date == null) {
            return 1;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }
}

Related

  1. sameYear(Date date1, Date date2)