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 int compare(Calendar date1, Calendar date2) {
    int i = date1.get(Calendar.YEAR);
    int j = date2.get(Calendar.YEAR);

    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.MONTH);
    j = date2.get(Calendar.MONTH);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.DAY_OF_MONTH);
    j = date2.get(Calendar.DAY_OF_MONTH);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.HOUR_OF_DAY);
    j = date2.get(Calendar.HOUR_OF_DAY);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.MINUTE);
    j = date2.get(Calendar.MINUTE);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.SECOND);
    j = date2.get(Calendar.SECOND);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    return 0;/*w ww.j ava 2  s . c  om*/
}

From source file:Main.java

public static String formatDate(Date date) {
    // TODO Auto-generated method stub
    Calendar calender = Calendar.getInstance();
    int thisyear = calender.get(Calendar.YEAR);
    calender.setTime(date);//from w w w.  j  a  v  a2 s .co m
    int dateyear = calender.get(Calendar.YEAR);
    if (thisyear == dateyear) {
        return DAY.format(date);
    }
    return DATE.format(date);
}

From source file:Main.java

/**
 * Method to check if a date is tomorrow
 * /*from w w w .j  av a  2s.c  o m*/
 * @param theDate
 *            the date to see if it is tomorrow
 * @return true of it is tomorrow
 */
public static boolean isTomorrow(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time and change its value so it
    // can be used in comparison with the given date.
    Calendar tmpDate = Calendar.getInstance();
    tmpDate.roll(Calendar.DAY_OF_YEAR, true);

    return tmpDate.get(Calendar.YEAR) == theCalendar.get(Calendar.YEAR)
            && tmpDate.get(Calendar.DAY_OF_YEAR) == theCalendar.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

private static int normalizeYear(int year) {
    if (year < 100 && year >= 0) {
        Calendar now = Calendar.getInstance();
        String currentYear = String.valueOf(now.get(Calendar.YEAR));
        String prefix = currentYear.substring(0, currentYear.length() - 2);
        year = Integer.parseInt(String.format(Locale.US, "%s%02d", prefix, year));
    }/*from  w w  w . j  a v a  2s .co m*/
    return year;
}

From source file:Main.java

/**
 * Method to see if a date has already passed. the time of the day has no
 * relevance.//w ww.  j  a va2 s .c om
 * 
 * @param theDate
 *            the date to see if it has passed
 * @return true if the date has passed
 */
public static boolean dateHasPassed(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time to compare with
    Calendar systemCalendar = Calendar.getInstance();
    // If it should return true only if today and not before use == instead
    // of >=
    return systemCalendar.get(Calendar.YEAR) > theCalendar.get(Calendar.YEAR)
            || (systemCalendar.get(Calendar.YEAR) == theCalendar.get(Calendar.YEAR)
                    && systemCalendar.get(Calendar.DAY_OF_YEAR) > theCalendar.get(Calendar.DAY_OF_YEAR));
}

From source file:Main.java

/**
 * Method to check if a date is today. Also returns true if the date is
 * earlier than today/*from  ww  w  . j a  v  a  2 s. c om*/
 * 
 * @param theDate
 *            the date to see if it is today
 * @return true if theDate is today or earlier
 */
public static boolean isToday(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time to compare with
    Calendar systemCalendar = Calendar.getInstance();
    // If it should return true only if today and not before use == instead
    // of >=
    return systemCalendar.get(Calendar.YEAR) >= theCalendar.get(Calendar.YEAR)
            && systemCalendar.get(Calendar.DAY_OF_YEAR) >= theCalendar.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

public static void moveToCalendarYear(Calendar cal, int year) {
    assertArgumentNotZeroInteger("year", year);
    if (year < 0) {
        year++; // BC headache
    }//from   w ww  .ja  v  a 2  s . c  om
    cal.set(Calendar.YEAR, year);
}

From source file:Main.java

public static double getJulDate() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);
    double extra = (100.0 * year) + month - 190002.5;
    double julianDay = (367.0 * year) - (Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0))
            + Math.floor((275.0 * month) / 9.0) + day + ((hour + ((minute + (second / 60.0)) / 60.0)) / 24.0)
            + 1721013.5 - ((0.5 * extra) / Math.abs(extra)) + 0.5;
    DecimalFormat sixDigitFormat = new DecimalFormat("#.######");
    return Double.valueOf(sixDigitFormat.format(julianDay));
}

From source file:Main.java

public static Bundle bundleCalendar(Calendar cal, long minDate) {
    Bundle args = new Bundle();
    if (cal == null)
        cal = Calendar.getInstance(Locale.getDefault());
    ;//from ww w.j  a  v a 2  s  . co m
    args.putInt("year", cal.get(Calendar.YEAR));
    args.putInt("month", cal.get(Calendar.MONTH));
    args.putInt("day", cal.get(Calendar.DAY_OF_MONTH));
    args.putInt("hour", cal.get(Calendar.HOUR_OF_DAY));
    args.putInt("minute", cal.get(Calendar.MINUTE));
    args.putLong("minDate", minDate);
    return args;
}

From source file:Main.java

public static long getPeriodEnd(int periodType, long date) {
    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(getPeriodStart(periodType, date));

    switch (periodType) {
    case TYPE_DAY: {
        cal.add(Calendar.DAY_OF_YEAR, 1);
        break;//ww w  . j a  v a2s . c o  m
    }

    case TYPE_WEEK: {
        cal.add(Calendar.WEEK_OF_YEAR, 1);
        break;
    }

    case TYPE_MONTH: {
        cal.add(Calendar.MONTH, 1);
        break;
    }

    case TYPE_YEAR: {
        cal.add(Calendar.YEAR, 1);
        break;
    }
    }

    cal.add(Calendar.MILLISECOND, -1);
    return cal.getTimeInMillis();
}