Java Utililty Methods Date to Year

List of utility methods to do Date to Year

Description

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

Method

int[]getYearMonthDay(Date date)
get Year Month Day
Calendar bgnDate = Calendar.getInstance();
bgnDate.setTime(date);
return new int[] { bgnDate.get(Calendar.YEAR), bgnDate.get(Calendar.MONTH) + 1,
        bgnDate.get(Calendar.DATE) };
StringgetYearMonthDay(Date date)
get Year Month Day
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
String month_str = (month < 10 ? ("0" + month) : month + "");
int day = calendar.get(Calendar.DAY_OF_MONTH);
String day_str = (day < 10 ? ("0" + day) : "" + day);
return year + "-" + month_str + "-" + day_str;
...
IntegergetYearMonthInt(Date date)
get Year Month Int
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return getYearMonthInt(String.valueOf(cal.get(Calendar.YEAR)), String.valueOf(cal.get(Calendar.MONTH) + 1));
StringgetYearMonthOfLastMon(Date date)
get Year Month Of Last Mon
Date newdate = getInternalDateByMon(date, -1);
String year = String.valueOf(getYear(newdate));
String month = String.valueOf(getMonth(newdate));
return year + month;
IntegergetYearNumber(Date date)
Retourne le nombre d'annee par rapport a la date du jour.
Integer yearNumber = null;
if (date != null) {
    Calendar dateYear = new GregorianCalendar();
    dateYear.setTime(date);
    Calendar today = Calendar.getInstance();
    int year = today.get(Calendar.YEAR) - dateYear.get(Calendar.YEAR);
    yearNumber = year;
return yearNumber;
intgetYearOfDate(Date date)
get Year Of Date
return getFieldValueByDate(date, Calendar.YEAR);
intgetYearOfThisYear(Date currentdate)
get Year Of This Year
Calendar cal = Calendar.getInstance();
if (currentdate != null) {
    cal.setTime(currentdate);
} else {
    cal.setTime(new Date());
return cal.get(Calendar.YEAR);
DategetYearOnlyDate(String year)
takes a string containing a 4 digit year and returns a Date object that has the year indicated and all other fields set to 0 (so jan 1 of that year at 00:00:00)
Calendar cal = new GregorianCalendar();
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.YEAR, new Integer(year));
return getDateNoTime(cal.getTime());
intgetYears(java.util.Date date0, java.util.Date date1)
get Years
Calendar calendar0 = Calendar.getInstance();
calendar0.setTime(date0);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
int year0 = calendar0.get(Calendar.YEAR);
int year1 = calendar1.get(Calendar.YEAR);
int years = year1 - year0;
return years;
...
DategetYearStart(Date date)
get Year Start
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal.getTime();
...