Example usage for java.util Calendar DAY_OF_YEAR

List of usage examples for java.util Calendar DAY_OF_YEAR

Introduction

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

Prototype

int DAY_OF_YEAR

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

Click Source Link

Document

Field number for get and set indicating the day number within the current year.

Usage

From source file:Main.java

private static boolean sameDay(Date a, Date b) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(a);//from w w w .  j a  v  a2 s  .c o m
    cal2.setTime(b);
    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

/**
 * Returns yesterday's date formatted according to the HTTP specification
 * standard date format.//ww w  . j  a  va 2 s. c  o  m
 *
 * @return a formatted string.
 */
public static String getExpiredHttpDateString() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_YEAR, -1);

    return getHttpDateString(cal.getTime());
}

From source file:Main.java

public static int getDateCompare(int nYear1, int nMonth1, int nDate1, int nYear2, int nMonth2, int nDate2) {
    Calendar cal = Calendar.getInstance();
    int nTotalDate1 = 0, nTotalDate2 = 0, nDiffOfYear = 0, nDiffOfDay = 0;

    if (nYear1 > nYear2) {
        for (int i = nYear2; i < nYear1; i++) {
            cal.set(i, 12, 0);//from   w w  w  .ja va 2  s . co m
            nDiffOfYear += cal.get(Calendar.DAY_OF_YEAR);
        }
        nTotalDate1 += nDiffOfYear;
    } else if (nYear1 < nYear2) {
        for (int i = nYear1; i < nYear2; i++) {
            cal.set(i, 12, 0);
            nDiffOfYear += cal.get(Calendar.DAY_OF_YEAR);
        }
        nTotalDate2 += nDiffOfYear;
    }

    cal.set(nYear1, nMonth1 - 1, nDate1);
    nDiffOfDay = cal.get(Calendar.DAY_OF_YEAR);
    nTotalDate1 += nDiffOfDay;

    cal.set(nYear2, nMonth2 - 1, nDate2);
    nDiffOfDay = cal.get(Calendar.DAY_OF_YEAR);
    nTotalDate2 += nDiffOfDay;

    return (nTotalDate1 - nTotalDate2) + 1;
}

From source file:Main.java

static int daysBetween(Calendar day1, Calendar day2) {
    /**/* w w  w  . j a va 2 s. c o  m*/
     * Saved some effort using the solution described here,
     * http://stackoverflow.com/a/28865648/1587370
     */
    Calendar dayOne = (Calendar) day1.clone(), dayTwo = (Calendar) day2.clone();

    if (dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR)) {
        return Math.abs(dayOne.get(Calendar.DAY_OF_YEAR) - dayTwo.get(Calendar.DAY_OF_YEAR));
    } else {
        if (dayTwo.get(Calendar.YEAR) > dayOne.get(Calendar.YEAR)) {
            //swap them
            Calendar temp = dayOne;
            dayOne = dayTwo;
            dayTwo = temp;
        }
        int extraDays = 0;

        int dayOneOriginalYearDays = dayOne.get(Calendar.DAY_OF_YEAR);

        while (dayOne.get(Calendar.YEAR) > dayTwo.get(Calendar.YEAR)) {
            dayOne.add(Calendar.YEAR, -1);
            // getActualMaximum() important for leap years
            extraDays += dayOne.getActualMaximum(Calendar.DAY_OF_YEAR);
        }

        return extraDays - dayTwo.get(Calendar.DAY_OF_YEAR) + dayOneOriginalYearDays;
    }
}

From source file:Main.java

/**
 * Determines the Date "date" adjusted by "days".
 * A negative value will return a date in the past.
 * @param date The date to be adjusted/* w ww.  j  a v  a2 s.  co m*/
 * @param days The number of days to adjust by
 * @return Returns a date object
 */
public static Date getDateAdjustedByDays(Date date, int days) {
    Calendar cal = Calendar.getInstance();
    if (date != null)
        cal.setTime(date);
    cal.add(Calendar.DAY_OF_YEAR, days);
    return cal.getTime();
}

From source file:Main.java

/**
 * @param days/*from  www. j a  v a  2s .  c  om*/
 *            number of days after September 02, 1975 (what happened on this
 *            day?)
 * @return
 */
public static Calendar getDayFromQuickLogEntry(int days) {
    Calendar logDay = GregorianCalendar.getInstance();
    logDay.set(Calendar.DAY_OF_MONTH, 2);
    logDay.set(Calendar.MONTH, Calendar.SEPTEMBER);
    logDay.set(Calendar.YEAR, 1975);
    logDay.add(Calendar.DAY_OF_YEAR, days);
    return logDay;
}

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;/* www .  ja v  a 2  s  .c  om*/
    }

    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();
}

From source file:Main.java

public static boolean sameDay(Date then, Date now) {

    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(then);/*from  w  w w  . ja  va 2 s  . c  om*/
    cal2.setTime(now);

    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

private static Date getNextBus(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/*from   w ww  . jav a 2s.  c o  m*/

    if (cal.get(Calendar.DAY_OF_WEEK) >= Calendar.MONDAY
            && cal.get(Calendar.DAY_OF_WEEK) <= Calendar.THURSDAY) {
        if (isBefore(cal, mon_thu[mon_thu.length - 1])) {
            setTime(cal, findNext(cal, mon_thu));
        } else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
            setTime(cal, fri[0]);
            cal.add(Calendar.DAY_OF_YEAR, 1);
        } else {
            setTime(cal, mon_thu[0]);
            cal.add(Calendar.DAY_OF_YEAR, 1);
        }
    } else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
        if (isBefore(cal, fri[fri.length - 1])) {
            setTime(cal, findNext(cal, fri));
        } else {
            setTime(cal, mon_thu[0]);
            cal.add(Calendar.DAY_OF_YEAR, 3);
        }
    } else {
        setTime(cal, mon_thu[0]);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    }
    return cal.getTime();
}

From source file:Main.java

/**
 * @param date/*  ww w .  j  a v  a2 s  . c  o m*/
 * @return
 */
public static String formatToYesterdayOrToday(Date date) {
    Calendar today = Calendar.getInstance();
    Calendar yesterday = Calendar.getInstance();
    yesterday.add(Calendar.DATE, -1);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm");

    if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)
            && calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) {
        return "Today, " + timeFormatter.format(date);
    } else if (calendar.get(Calendar.YEAR) == yesterday.get(Calendar.YEAR)
            && calendar.get(Calendar.DAY_OF_YEAR) == yesterday.get(Calendar.DAY_OF_YEAR)) {
        return "Yesterday, " + timeFormatter.format(date);
    } else {
        return DateFormat.format("MMM dd, ", date).toString() + timeFormatter.format(date);

    }
}