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:gr.abiss.calipso.hibernate.HibernateDao.java

/**
 * Returns an iterator for items due within 24 hours.
 *//*from   www.j  a  v  a 2 s.  c  o m*/
@Override
public Iterator<Item> findItemsDueIn24Hours() {
    // get a calendar instance, which defaults to "now"
    Calendar calendar = Calendar.getInstance();
    // add one day to the date/calendar
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    // now get "tomorrow"
    Date thisTimeTommorow = calendar.getTime();
    // we prefer an iterator VS a list or other collection as it is more efficient for larger datasets and batch jobs in general
    return getHibernateTemplate().iterate(
            "from Item item where item.sentDueToNotifications = false and (item.dueTo < ? or item.stateDueTo < ? )",
            new Object[] { thisTimeTommorow, thisTimeTommorow });
}

From source file:edu.uga.cs.fluxbuster.features.FeatureCalculator.java

/**
 * Gets run dates previous to a specific date within a window
 * of days from that date.// ww w  .  j  a  v  a2 s . c om
 *
 * @param log_date the run date
 * @param window the number of days previous to the current date
 * @return the list of previous run dates
 * @throws SQLException if there is an error retrieving the previous
 *       run dates
 */
public ArrayList<Date> getPrevDates(Date log_date, int window) throws SQLException {
    ArrayList<Date> prevDates = new ArrayList<Date>();
    if (prevDateBufDate != null && prevDateBuf != null && prevDateBufDate.equals(log_date)
            && prevDateBufWindow >= window) {

        //pull the dates within the day window from the prevDateBuf cache
        Date pd = null;
        int windowcount = 0;
        for (Date d : prevDateBuf) {
            if (windowcount >= window) {
                break;
            }
            if (pd == null) {
                pd = d;
                windowcount++;
            } else {
                DateTime morerecent = new DateTime(d.getTime());
                DateTime lessrecent = new DateTime(pd.getTime());
                Days days = Days.daysBetween(morerecent, lessrecent);
                windowcount += days.getDays();
                pd = d;
            }
            prevDates.add(d);
        }

    } else {
        String domainsprefix = properties.getProperty(DOMAINSPREFIXKEY);
        String resipsprefix = properties.getProperty(RESIPSPREFIXKEY);

        ArrayList<String> tablenames = new ArrayList<String>();
        ResultSet rs1 = null;
        try {
            rs1 = dbi.executeQueryWithResult(properties.getProperty(TABLES_QUERY1KEY));
            while (rs1.next()) {
                tablenames.add(rs1.getString(1));
            }
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error(e);
            }
        } finally {
            if (rs1 != null && !rs1.isClosed()) {
                rs1.close();
            }
        }

        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(log_date);
        for (int i = 0; i < window; i++) {
            cal.roll(Calendar.DAY_OF_YEAR, false);
            Date temp = cal.getTime();
            String datestr = df.format(temp);
            if (tablenames.contains(domainsprefix + "_" + datestr)
                    && tablenames.contains(resipsprefix + "_" + datestr)) {
                prevDates.add(temp);
            }
        }

        //cache the values for later
        if (prevDateBuf == null) {
            prevDateBuf = new ArrayList<Date>();
        } else {
            prevDateBuf.clear();
        }
        prevDateBuf.addAll(prevDates);
        prevDateBufDate = log_date;
        prevDateBufWindow = window;
    }
    return prevDates;
}

From source file:de.ribeiro.android.gso.core.UntisProvider.java

private static boolean AlreadyContainsDay(List<ICalEvent> events, ICalEvent event) {
    for (ICalEvent ev : events) {
        if (ev.DTSTART == null || event.DTSTART == null)
            break;
        if (ev.DTSTART.get(Calendar.DAY_OF_YEAR) == event.DTSTART.get(Calendar.DAY_OF_YEAR))
            return true;
    }/*from   w w  w  .j a va  2s  .com*/
    return false;
}

From source file:Dates.java

/**
 * Date Arithmetic function (date2 - date1). subtract a date from another
 * date, return the difference as the required fields. E.g. if specified
 * Calendar.Date, the smaller range of fields is ignored and this method
 * return the difference of days.//w  w  w  . j a  va 2s  .  c o m
 * 
 * @param date2
 *          The date2.
 * @param tz
 *          The time zone.
 * @param field
 *          The time field; e.g., Calendar.DATE, Calendar.YEAR, it's default
 *          value is Calendar.DATE
 * @param date1
 *          The date1.
 */
public static final long subtract(Date date2, TimeZone tz, int field, Date date1) {
    if (tz == null)
        tz = TimeZones.getCurrent();

    boolean negative = false;
    if (date1.after(date2)) {
        negative = true;
        final Date d = date1;
        date1 = date2;
        date2 = d;
    }

    final Calendar cal1 = Calendar.getInstance(tz);
    cal1.setTimeInMillis(date1.getTime());// don't call cal.setTime(Date) which
                                          // will reset the TimeZone.

    final Calendar cal2 = Calendar.getInstance(tz);
    cal2.setTimeInMillis(date2.getTime());// don't call cal.setTime(Date) which
                                          // will reset the TimeZone.

    int year1 = cal1.get(Calendar.YEAR);
    int year2 = cal2.get(Calendar.YEAR);

    switch (field) {
    case Calendar.YEAR: {
        return negative ? (year1 - year2) : (year2 - year1);
    }
    case Calendar.MONTH: {
        int month1 = cal1.get(Calendar.MONTH);
        int month2 = cal2.get(Calendar.MONTH);
        int months = 12 * (year2 - year1) + month2 - month1;
        return negative ? -months : months;
    }
    case Calendar.HOUR: {
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        long min1 = (time1 < 0 ? (time1 - (1000 * 60 * 60 - 1)) : time1) / (1000 * 60 * 60);
        long min2 = (time2 < 0 ? (time2 - (1000 * 60 * 60 - 1)) : time2) / (1000 * 60 * 60);
        return negative ? (min1 - min2) : (min2 - min1);
    }
    case Calendar.MINUTE: {
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        long min1 = (time1 < 0 ? (time1 - (1000 * 60 - 1)) : time1) / (1000 * 60);
        long min2 = (time2 < 0 ? (time2 - (1000 * 60 - 1)) : time2) / (1000 * 60);
        return negative ? (min1 - min2) : (min2 - min1);
    }
    case Calendar.SECOND: {
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        long sec1 = (time1 < 0 ? (time1 - (1000 - 1)) : time1) / 1000;
        long sec2 = (time2 < 0 ? (time2 - (1000 - 1)) : time2) / 1000;

        return negative ? (sec1 - sec2) : (sec2 - sec1);
    }
    case Calendar.MILLISECOND: {
        return negative ? (date1.getTime() - date2.getTime()) : (date2.getTime() - date1.getTime());
    }
    case Calendar.DATE:
    default: /* default, like -1 */
    {
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int maxDay1 = year1 == year2 ? 0 : cal1.getActualMaximum(Calendar.DAY_OF_YEAR);
        int days = maxDay1 - day1 + day2;

        final Calendar cal = Calendar.getInstance(tz);
        for (int year = year1 + 1; year < year2; year++) {
            cal.set(Calendar.YEAR, year);
            days += cal.getActualMaximum(Calendar.DAY_OF_YEAR);
        }
        return negative ? -days : days;
    }
    }
}

From source file:com.eryansky.common.utils.SysUtils.java

public static String dateFormatTallyCN() {
     Calendar calendar = Calendar.getInstance();
     calendar.add(Calendar.DAY_OF_YEAR, -1);
     return dateFormat(calendar.getTime(), "yyyyMMdd");
 }

From source file:org.openmrs.module.rwandaprimarycare.RwandaPrimaryCarePatientDashboardController.java

@RequestMapping(value = "/module/rwandaprimarycare/visitDate", method = RequestMethod.GET)
public String visitDate(@RequestParam("patientId") int patientId, HttpSession session, ModelMap model)
        throws PrimaryCareException {
    //LK: Need to ensure that all primary care methods only throw a PrimaryCareException
    //So that errors will be directed to a touch screen error page
    try {/*from ww  w .ja  v a 2s  .  c  om*/
        Calendar todaysDate = Calendar.getInstance();
        Integer backEntry = Integer
                .valueOf(Context.getAdministrationService().getGlobalProperty("registration.backEntryLimit"));

        List<Date> dates = new ArrayList<Date>();

        for (int i = 0; i < backEntry; i++) {
            todaysDate.add(Calendar.DAY_OF_YEAR, -1);
            Date date = todaysDate.getTime();
            dates.add(date);
        }

        model.addAttribute("dates", dates);

        return "/module/rwandaprimarycare/visitDate";
    } catch (Exception e) {
        throw new PrimaryCareException(e);
    }
}

From source file:es.ficonlan.web.backend.test.eventservice.EventServiceTest.java

@Test
public void testGetActivityParticipants() throws ServiceException {
    Session anonymousSession = userService.newAnonymousSession();
    Session s = userService.login(anonymousSession.getSessionId(), ADMIN_LOGIN, ADMIN_PASS);
    User user1 = userService.addUser(anonymousSession.getSessionId(),
            new User("User1", "login1", "pass", "12345678R", "user1@gmail.com", "690047407", "L"));
    User user2 = userService.addUser(anonymousSession.getSessionId(),
            new User("User2", "login2", "pass", "12332218R", "user2@gmail.com", "690047407", "L"));
    Calendar dateStart = Calendar.getInstance();
    dateStart.add(Calendar.DAY_OF_YEAR, -1);
    Calendar dateEnd = Calendar.getInstance();
    dateEnd.add(Calendar.DAY_OF_YEAR, 1);

    Event event = eventService.createEvent(s.getSessionId(), new Event(0, "FicOnLan 2014", "FicOnLan 2014", 150,
            dateStart, dateEnd, dateStart, dateEnd, null, null, null, null, null));
    eventService.addParticipantToEvent(s.getSessionId(), user1.getUserId(), event.getEventId());
    eventService.addParticipantToEvent(s.getSessionId(), user2.getUserId(), event.getEventId());
    Activity activity = eventService.addActivity(s.getSessionId(), event.getEventId(),
            new Activity(event, "Torneo de Lol", "Torneo de Lol", 10, ActivityType.Tournament, true, dateStart,
                    dateEnd, dateStart, dateEnd));
    eventService.addParticipantToActivity(s.getSessionId(), user1.getUserId(), activity.getActivityId());
    eventService.addParticipantToActivity(s.getSessionId(), user2.getUserId(), activity.getActivityId());
    List<User> result = eventService.getActivityParticipants(s.getSessionId(), activity.getActivityId());
    assertTrue(result.size() == 2);//from w w  w  .  j  a v a 2 s  .  co m
    assertTrue(result.get(0).getUserId() == user1.getUserId());
    assertTrue(result.get(1).getUserId() == user2.getUserId());
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static Object getDayNumber(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    if (ArgList.length == 2) {
        try {//from w w w .  j  a va  2  s.  co m
            if (isNull(ArgList[0]))
                return new Double(Double.NaN);
            else if (isUndefined(ArgList[0]))
                return undefinedValue;
            else {
                java.util.Date dIn = (java.util.Date) ArgList[0];
                String strType = (String) ArgList[1];
                Calendar startDate = Calendar.getInstance();
                startDate.setTime(dIn);
                if (strType.toLowerCase().equals("y"))
                    return new Double(startDate.get(Calendar.DAY_OF_YEAR));
                else if (strType.toLowerCase().equals("m"))
                    return new Double(startDate.get(Calendar.DAY_OF_MONTH));
                else if (strType.toLowerCase().equals("w"))
                    return new Double(startDate.get(Calendar.DAY_OF_WEEK));
                else if (strType.toLowerCase().equals("wm"))
                    return new Double(startDate.get(Calendar.DAY_OF_WEEK_IN_MONTH));
                return new Double(startDate.get(Calendar.DAY_OF_YEAR));
            }
        } catch (Exception e) {
            return null;
            //throw new RuntimeException(e.toString());      
        }
    } else {
        throw new RuntimeException("The function call getDayNumber requires 2 arguments.");
    }
}

From source file:Time.java

/**
 * Return the number of days in the year of this day.
 * /*from w  w w .  ja  v  a 2  s .co  m*/
 * @return Number of days in this year.
 */
public int getDaysInYear() {
    return calendar_.getActualMaximum(Calendar.DAY_OF_YEAR);
}

From source file:com.smarthome.deskclock.Alarms.java

/**
 * Given an alarm in hours and minutes, return a time suitable for
 * setting in AlarmManager.//  w  ww . j a  v a2  s.  co  m
 */
static Calendar calculateAlarm(int hour, int minute, Alarm.DaysOfWeek daysOfWeek) {

    // start with now
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());

    int nowHour = c.get(Calendar.HOUR_OF_DAY);
    int nowMinute = c.get(Calendar.MINUTE);

    // if alarm is behind current time, advance one day
    if (hour < nowHour || hour == nowHour && minute <= nowMinute) {
        c.add(Calendar.DAY_OF_YEAR, 1);
    }
    c.set(Calendar.HOUR_OF_DAY, hour);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);

    int addDays = daysOfWeek.getNextAlarm(c);
    if (addDays > 0)
        c.add(Calendar.DAY_OF_WEEK, addDays);
    return c;
}