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

/**
 * Method to check if a date is tomorrow
 * //from w w  w.j a v a 2s  .co  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

public static boolean isSameDay(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);//from   w w w.ja  va  2  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 String getAge(String fec_nac) {

    DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
    Calendar dob = Calendar.getInstance();

    Date d = null;//from   w ww .  j a v a  2s .com
    try {
        d = df.parse(fec_nac);

    } catch (Exception e) {
    }

    Calendar today = Calendar.getInstance();
    dob.setTime(d);

    int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

    if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
        age--;
    }
    return String.valueOf(age);
}

From source file:Main.java

public static long getTomorrowZeroTimeInMillis() {
    Calendar calendar = getZeroSecondCalendar();
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    return calendar.getTimeInMillis();
}

From source file:Main.java

/**
 * Same as the getCurrentDay() method but uses the passed in argument
 * as a long milliseconds timestamp to calculate vs a day object
 * @parma timestamp This is the time (in milliseconds from the epoch date)
 * used to calculate the current day of the year. (IE 365 = Dec 31st)
 * @return Day Day of the year based on timestamp.
 *//*from   www.j  av  a2  s. c  o m*/
public static Integer getCurrentDay(long timestamp) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(timestamp);
    return cal.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 ava  2 s .  c o m
 * 
 * @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 long getPeriodStart(int periodType, long date) {
    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(date);//from  w w  w.ja v  a  2 s.  co  m

    switch (periodType) {
    case TYPE_DAY: {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_WEEK: {
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        final int currentDayOfWeek = (cal.get(Calendar.DAY_OF_WEEK) + 7 - cal.getFirstDayOfWeek()) % 7;
        cal.add(Calendar.DAY_OF_YEAR, -currentDayOfWeek);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_MONTH: {
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_YEAR: {
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }
    }
    return cal.getTimeInMillis();
}

From source file:Main.java

public static boolean isSameDay(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
        throw new IllegalArgumentException("The date must not be null");
    }//from ww  w.j  a v  a  2s .c  om
    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

/**
 * Builds a simple hash for a day by concatenating year and day of year together. Note that two
 * {@link java.util.Calendar} inputs that fall on the same day will be hashed to the same
 * string.//from  ww w  . j a  va 2 s. c om
 */
public static String getHashedDay(Calendar day) {
    return day.get(Calendar.YEAR) + "-" + day.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

/**
 * Calculates the difference in days, rounding the operands down to the nearest day.
 * Ex. Jan 3rd 12:31 pm - Jan 2nd 4:00 pm
 * =   Jan 3rd - Jan 2nd/*from  w  ww  .ja  v  a  2  s  .c o m*/
 * =   1 day 
 * @return The difference in days.
 */
public static int differenceInDays(GregorianCalendar minuend, GregorianCalendar subtrahend) {
    GregorianCalendar minuendFloor = new GregorianCalendar(minuend.get(Calendar.YEAR),
            minuend.get(Calendar.MONTH), minuend.get(Calendar.DAY_OF_MONTH));
    GregorianCalendar subtrahendFloor = new GregorianCalendar(subtrahend.get(Calendar.YEAR),
            subtrahend.get(Calendar.MONTH), subtrahend.get(Calendar.DAY_OF_MONTH));
    GregorianCalendar result = new GregorianCalendar();
    result.setTimeInMillis(minuendFloor.getTimeInMillis() - subtrahendFloor.getTimeInMillis());
    return result.get(Calendar.DAY_OF_YEAR);
}