Java Year sameYear(Date date1, Date date2)

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

Description

same Year

License

Apache License

Parameter

Parameter Description
date1 a date
date2 another date

Return

true of date1 and date2 are in the same year, false otherwise.

Declaration

public static boolean sameYear(Date date1, Date date2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**/*  w w w  .j  a va2s  .  c o  m*/
     * @param date1 a date
     * @param date2 another date
     * @return <code>true</code> of <code>date1</code> and <code>date2</code> are in the same year,
     *         <code>false</code> otherwise.
     */
    public static boolean sameYear(Date date1, Date date2) {
        if (date1 == null || date2 == null)
            throw new IllegalArgumentException("'date1' or 'date2' was null");

        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);

        return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR);
    }
}

Related

  1. differenceInYears(final Calendar a, final Calendar b)
  2. diffYears(Date day1, Date day2)
  3. IsInLeapYear(Date date1)
  4. isLeapYear(int year)
  5. nextYears(int diff)
  6. yearAndSeason(Date date)