Java Utililty Methods Year

List of utility methods to do Year

Description

The list of methods to do Year are organized into topic(s).

Method

CalendarcreateCalendarAtBeginningOfYear(int year)
create Calendar At Beginning Of Year
final GregorianCalendar calendar = createUtcCalendar();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
...
IntegercurrentYear()
current Year
GregorianCalendar gFechaActual = new GregorianCalendar();
return gFechaActual.get(GregorianCalendar.YEAR);
intdaysInYear(int year)
days In Year
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
return c.getActualMaximum(Calendar.DAY_OF_YEAR);
doubledifferenceInYears(final Calendar a, final Calendar b)
Calculates difference in years between to dates.
final long millisDif = a.getTimeInMillis() - b.getTimeInMillis();
return millisDif / MILLISECONDS_PER_YEAR;
intdiffYears(Date day1, Date day2)
diff Years
Calendar cal = Calendar.getInstance();
cal.setTime(day1);
int startYear = cal.getWeekYear();
cal.clear();
cal.setTime(day2);
int endYear = cal.getWeekYear();
int diffyears = endYear - startYear;
return diffyears;
...
booleanIsInLeapYear(Date date1)
Indicates whether the year in the specified date is in a leap year.
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date1);
return cal.isLeapYear(cal.get(cal.YEAR));
booleanisLeapYear(int year)
is Leap Year
Calendar calendar = Calendar.getInstance();
calendar.set(year, 2, 1);
calendar.add(Calendar.DATE, -1);
if (calendar.get(Calendar.DAY_OF_MONTH) == 29) {
    return true;
} else {
    return false;
DatenextYears(int diff)
next Years
return add(new Date(), Calendar.YEAR, diff);
booleansameYear(Date date1, Date date2)
same Year
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);
StringyearAndSeason(Date date)
year And Season
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
return yearAndSeason(cal);