Example usage for java.util Calendar YEAR

List of usage examples for java.util Calendar YEAR

Introduction

In this page you can find the example usage for java.util Calendar YEAR.

Prototype

int YEAR

To view the source code for java.util Calendar YEAR.

Click Source Link

Document

Field number for get and set indicating the year.

Usage

From source file:Main.java

public static boolean sameDate(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);/*  w  w  w .j a  va2 s . c o m*/
    cal2.setTime(date2);
    boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
    return sameDay;
}

From source file:Main.java

public static boolean isSameDay(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null)
        throw new IllegalArgumentException("The dates must not be null");
    return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA)
            && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}

From source file:Main.java

public static boolean isSameDay(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);/*from w ww .j  a  v a 2s . c om*/
    cal2.setTime(date2);
    boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
    return sameDay;
}

From source file:Main.java

public static int getDiffOfYears(Date starDate, Date endDate) {
    Calendar star = Calendar.getInstance();
    star.setTime(starDate);/*from ww  w  . ja v a2s  .c o m*/
    Calendar end = Calendar.getInstance();
    end.setTime(endDate);
    int diffYear = star.get(Calendar.YEAR) - end.get(Calendar.YEAR);
    return Math.abs(diffYear);
}

From source file:Main.java

public static int getUserAge(Date birthDay) {
    Calendar cal = Calendar.getInstance();

    if (cal.before(birthDay)) {
        return 0;
    }//from   ww w. ja  va 2  s  . c  o  m
    int yearNow = cal.get(Calendar.YEAR);
    int monthNow = cal.get(Calendar.MONTH);
    int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
    if (null == birthDay) {
        return 0;
    } else {
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                // monthNow==monthBirth
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                } else {
                    // do nothing
                }
            } else {
                // monthNow>monthBirth
                age--;
            }
        } else {
            // monthNow<monthBirth
            // donothing
        }
        return age;
    }
}

From source file:Main.java

public static boolean isSameDate(long d1, long d2) {
    Calendar cal1 = Calendar.getInstance();
    cal1.setTimeInMillis(d1);/*  w ww.j av a2s  .c om*/
    Calendar cal2 = Calendar.getInstance();
    cal2.setTimeInMillis(d2);
    return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

public static int compareDate(Calendar time1, Calendar time2) {
    int result = -1;
    if (time1.getTimeInMillis() == time2.getTimeInMillis()) {
        result = 0;/*from   w  ww  . j  a v  a 2  s  . co m*/
    } else if (time1.get(Calendar.YEAR) == time2.get(Calendar.YEAR)) {
        result = 6;
        if (time1.get(Calendar.MONTH) == time2.get(Calendar.MONDAY)) {
            result = 5;
            if (time1.get(Calendar.DAY_OF_MONTH) == time2.get(Calendar.DAY_OF_MONTH)) {
                result = 4;
                if (time1.get(Calendar.HOUR_OF_DAY) == time2.get(Calendar.HOUR_OF_DAY)) {
                    result = 3;
                    if (time1.get(Calendar.MINUTE) == time2.get(Calendar.MINUTE)) {
                        result = 2;
                        if (time1.get(Calendar.SECOND) == time2.get(Calendar.SECOND)) {
                            result = 1;
                        }
                    }
                }
            }
        }
    } else if (time1.get(Calendar.YEAR) / 100 == time2.get(Calendar.YEAR) / 100) {
        result = 7;
    }
    return result;
}

From source file:Main.java

public static boolean isToday(Calendar cal) {

    Calendar today = Calendar.getInstance(Locale.getDefault());

    return cal.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)
            && cal.get(Calendar.YEAR) == today.get(Calendar.YEAR);
}

From source file:Main.java

public static boolean isDatesInSameWeek(long lhsDate, long rhsDate) {
    Calendar lhsCalendar = getCalendarWithTime(lhsDate);
    int lhsWeek = lhsCalendar.get(Calendar.WEEK_OF_YEAR);
    int lhsYear = lhsCalendar.get(Calendar.YEAR);

    Calendar rhsCalendar = getCalendarWithTime(rhsDate);
    int rhsWeek = rhsCalendar.get(Calendar.WEEK_OF_YEAR);
    int rhsYear = rhsCalendar.get(Calendar.YEAR);

    return (lhsWeek == rhsWeek) && (lhsYear == rhsYear);
}

From source file:Main.java

/**
 * the first day of month//from   ww w.  j a  v  a 2s . com
 * @param year
 * @param month
 * @return week position
 */
public static int getDayOfWeekInMonth(int year, int month) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month - 1);
    calendar.set(Calendar.DATE, 1);
    return calendar.get(Calendar.DAY_OF_WEEK);
}