Example usage for java.util Calendar DAY_OF_WEEK

List of usage examples for java.util Calendar DAY_OF_WEEK

Introduction

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

Prototype

int DAY_OF_WEEK

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

Click Source Link

Document

Field number for get and set indicating the day of the week.

Usage

From source file:Holidays.java

public static java.util.Calendar USElectionDay(int nYear) {
    // First Tuesday in November
    int nX;/*from  w ww.  j a va 2 s .c  o  m*/
    int nMonth = 10; // November
    java.util.Calendar cal = java.util.Calendar.getInstance();
    cal.set(nYear, nMonth, 1);
    nX = cal.get(Calendar.DAY_OF_WEEK);
    switch (nX) {
    case 0: {// Sunday
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 3);
        return cal;
    }
    case 1: {// Monday
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 2);
        return cal;
    }
    case 2: // Tuesday
    {
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 1);
        return cal;
    }
    case 3: // Wednesday
    {
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 7);
        return cal;
    }
    case 4: // Thrusday
    {
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 6);
        return cal;
    }
    case 5: // Friday
    {
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 5);
        return cal;
    }
    default: // Saturday
    {
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 4);
        return cal;
    }
    }
}

From source file:org.everit.jira.timetracker.plugin.JiraTimetrackerPluginImpl.java

@Override
public List<Date> getDates(final String selectedUser, final Date from, final Date to, final boolean workingHour,
        final boolean checkNonWorking) throws GenericEntityException {
    List<Date> datesWhereNoWorklog = new ArrayList<Date>();
    Calendar fromDate = Calendar.getInstance();
    fromDate.setTime(from);/*from  w  w  w.  java2s  .c  o  m*/
    Calendar toDate = Calendar.getInstance();
    toDate.setTime(to);
    while (!fromDate.after(toDate)) {
        String currentDateString = DateTimeConverterUtil.dateToString(fromDate.getTime());
        if (excludeDatesSet.contains(currentDateString)) {
            fromDate.add(Calendar.DATE, 1);
            continue;
        }
        // check includes - not check weekend
        // check weekend - pass
        if (!includeDatesSet.contains(currentDateString)
                && ((fromDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
                        || (fromDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY))) {
            fromDate.add(Calendar.DATE, 1);
            continue;
        }
        // check worklog. if no worklog set result else ++ scanedDate
        boolean isDateContainsWorklog;
        if (workingHour) {
            isDateContainsWorklog = isContainsEnoughWorklog(fromDate.getTime(), checkNonWorking);
        } else {
            isDateContainsWorklog = isContainsWorklog(fromDate.getTime());
        }
        if (!isDateContainsWorklog) {
            datesWhereNoWorklog.add((Date) fromDate.getTime().clone());
        }
        fromDate.add(Calendar.DATE, 1);

    }
    Collections.reverse(datesWhereNoWorklog);
    return datesWhereNoWorklog;
}

From source file:com.aurel.track.linkType.MsProjectLinkTypeBL.java

/**
 * Returns number of sundays from interval. Start date included always, endDate only if includeLastDay == true!
 * @param startDateParam/*from   w  w  w. j a  v a 2 s .  c o m*/
 * @param endDateParam
 * @param includeLastDay
 * @return
 */
public static Integer getNumberOfSundaysFromInterval(Date startDateParam, Date endDateParam,
        boolean includeLastDay) {
    Date realEndDate = endDateParam;
    if (includeLastDay) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(realEndDate);
        cal.add(Calendar.DAY_OF_YEAR, 1);
        realEndDate = cal.getTime();
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(startDateParam);
    int numOfSunday = 0;
    while (!calendar.getTime().after(realEndDate)) {
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            numOfSunday++;
        }
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
    return numOfSunday;
}

From source file:fr.paris.lutece.plugins.suggest.utils.SuggestUtils.java

/**
 * return the last day of week function of the date .
 * @param date the date/*from   w  w  w.  j ava  2  s. c om*/
 * @return the last day of week function of the date.
 */
public static Timestamp getLastDayOfWeek(Timestamp date) {
    Calendar caldate = new GregorianCalendar();
    caldate.setTime(date);
    caldate.set(Calendar.MILLISECOND, caldate.getActualMaximum(Calendar.MILLISECOND));
    caldate.set(Calendar.SECOND, caldate.getActualMaximum(Calendar.SECOND));
    caldate.set(Calendar.HOUR_OF_DAY, caldate.getActualMaximum(Calendar.HOUR_OF_DAY));
    caldate.set(Calendar.MINUTE, caldate.getActualMaximum(Calendar.MINUTE));
    caldate.set(Calendar.DAY_OF_WEEK, caldate.getFirstDayOfWeek() + 6);

    Timestamp timeStamp = new Timestamp(caldate.getTimeInMillis());

    return timeStamp;
}

From source file:jef.tools.DateUtils.java

/**
 * /*  w  ww  .  jav  a 2  s.c om*/
 * 
 * @param d
 * @return 0: ,1~6 
 *         ?Calendarsunday???1?
 *         null-1
 */
public static int getWeekDay(Date d) {
    if (d == null)
        return -1;
    final Calendar c = new GregorianCalendar();
    c.setTime(d);
    return c.get(Calendar.DAY_OF_WEEK) - 1;
}

From source file:com.feilong.commons.core.date.DateUtil.java

/**
 * ?<br>//from www  .  j  ava 2  s . c om
 * ,1.
 * 
 * <pre>
 * SUNDAY?MONDAY?TUESDAY?WEDNESDAY?THURSDAY?FRIDAY  SATURDAY
 * 
 * 1-7
 * </pre>
 * 
 * <pre>
 * 2012-6-29    5
 * return 6
 * </pre>
 * 
 * @param date
 *            the date
 * @return ?
 * @see Calendar#SUNDAY
 * @see Calendar#MONDAY
 * @see Calendar#TUESDAY
 * @see Calendar#WEDNESDAY
 * @see Calendar#THURSDAY
 * @see Calendar#FRIDAY
 * @see Calendar#SATURDAY
 * @see CalendarUtil#getCalendarFieldValue(Date, int)
 * @see Calendar#DAY_OF_WEEK
 */
public static final int getDayOfWeek(Date date) {
    return CalendarUtil.getCalendarFieldValue(date, Calendar.DAY_OF_WEEK);
}

From source file:com.radvision.icm.service.vcm.ICMService.java

private static RecurrenceInfo convertToRecurringInfo(RecurringMeetingInfo rmi, List<String> listTerminalId,
        boolean isCreating) throws Exception {

    RecurrenceInfo ri = new RecurrenceInfo();

    ConferenceInfo info = new ConferenceInfo();
    info.setConferenceId(isCreating ? null : rmi.getRadRecurrenceId());
    info.setUserID(rmi.getUserId());/*from  w ww . j  a  v  a  2s  .  co  m*/
    info.setOrgID(rmi.getMemberId());
    info.setDialableConferenceId(rmi.getDialableNumber());
    long startTime = rmi.getStartTime();
    long endTime = startTime + rmi.getTimeLong() * 60000;
    info.setStartTime(startTime);
    info.setEndTime(endTime);
    info.setMeetingTypeId(rmi.getServiceTemplateId());
    info.setDescription(rmi.getDescription());
    info.setPassword(rmi.getPassword());
    info.setFullControlPassword(rmi.getControlPin());
    int reservedport = rmi.getPortsNum() == null ? 2 : rmi.getPortsNum();
    info.setReservedIPPorts(reservedport);
    // info.setReservedISDNPorts(reservedport);
    info.setSubject(rmi.getSubject());
    if (listTerminalId != null && listTerminalId.size() > 0) {
        List<TerminalInfo> terminals = info.getTerminals();
        for (int i = 0; i < listTerminalId.size(); i++) {
            TerminalInfo ti = new TerminalInfo();
            ti.setTerminalId(listTerminalId.get(i));
            terminals.add(ti);
        }
    }
    ri.setConferenceInfoTemplate(info);

    List<RecurInstanceInfo> riis = ri.getRecurInstanceInfos();
    if (rmi.getRecurrenceType() == RecurringMeetingInfo.RECURRING_DAILY) {
        int interval = rmi.getDayInterval();
        long currTime = System.currentTimeMillis();
        long nextTime = (startTime > currTime) ? startTime : currTime;
        rmi.setStartDate(getCurrentDate(nextTime));
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(nextTime);
        if (rmi.getEndType() == RecurringMeetingInfo.RECURRING_ENDTYPE_DATE) {
            long endDate = rmi.getEndDate();
            rmi.setEndDate(getCurrentDate(endDate));
            while (nextTime < endDate) {
                if (interval <= 0) {
                    int weekDay = cal.get(Calendar.DAY_OF_WEEK);
                    if (weekDay == 0 && weekDay == 6) {
                        cal.add(Calendar.DAY_OF_YEAR, 1);
                        nextTime = cal.getTimeInMillis();
                        continue;
                    }
                }
                RecurInstanceInfo rii = new RecurInstanceInfo();
                rii.setStartTime(nextTime);
                rii.setEndTime(nextTime + rmi.getTimeLong() * 60000);
                riis.add(rii);
                cal.add(Calendar.DAY_OF_YEAR, interval <= 0 ? 1 : interval);
                nextTime = cal.getTimeInMillis();
            }
        } else {
            int repeat = rmi.getEndAfterNumber();
            int i = 0;
            while (i < repeat) {
                if (interval <= 0) {
                    int weekDay = cal.get(Calendar.DAY_OF_WEEK);
                    if (weekDay == 0 && weekDay == 6) {
                        cal.add(Calendar.DAY_OF_YEAR, 1);
                        nextTime = cal.getTimeInMillis();
                        continue;
                    }
                }
                RecurInstanceInfo rii = new RecurInstanceInfo();
                rii.setStartTime(nextTime);
                rii.setEndTime(nextTime + rmi.getTimeLong() * 60000);
                riis.add(rii);
                i++;
                if (i >= repeat) {
                    rmi.setEndDate(getCurrentDate(nextTime));
                    break;
                }
                cal.add(Calendar.DAY_OF_YEAR, interval <= 0 ? 1 : interval);
                nextTime = cal.getTimeInMillis();
            }
        }
    } else if (rmi.getRecurrenceType() == RecurringMeetingInfo.RECURRING_WEEKLY) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(startTime);
        int startDay = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_YEAR, rmi.getWeekDay() - startDay);
        startTime = cal.getTimeInMillis();
        long currTime = System.currentTimeMillis();
        long nextTime = startTime;
        int interval = rmi.getWeekInterval();
        if (startTime < currTime) {
            cal.add(Calendar.DAY_OF_YEAR, 7 * interval);
            nextTime = cal.getTimeInMillis();
        }
        rmi.setStartDate(getCurrentDate(nextTime));
        if (rmi.getEndType() == RecurringMeetingInfo.RECURRING_ENDTYPE_DATE) {
            long endDate = rmi.getEndDate();
            rmi.setEndDate(getCurrentDate(endDate));
            while (nextTime < endDate) {
                if (interval <= 0) {
                    int weekDay = cal.get(Calendar.DAY_OF_WEEK);
                    if (weekDay == 0 && weekDay == 6) {
                        cal.add(Calendar.DAY_OF_YEAR, 1);
                        nextTime = cal.getTimeInMillis();
                        continue;
                    }
                }
                RecurInstanceInfo rii = new RecurInstanceInfo();
                rii.setStartTime(nextTime);
                rii.setEndTime(nextTime + rmi.getTimeLong() * 60000);
                riis.add(rii);
                cal.add(Calendar.DAY_OF_YEAR, interval <= 0 ? 1 : interval);
                nextTime = cal.getTimeInMillis();
            }
        } else {
            int repeat = rmi.getEndAfterNumber();
            int i = 0;
            while (i < repeat) {
                if (interval <= 0) {
                    int weekDay = cal.get(Calendar.DAY_OF_WEEK);
                    if (weekDay == 0 && weekDay == 6) {
                        cal.add(Calendar.DAY_OF_YEAR, 1);
                        nextTime = cal.getTimeInMillis();
                        continue;
                    }
                }
                RecurInstanceInfo rii = new RecurInstanceInfo();
                rii.setStartTime(nextTime);
                rii.setEndTime(nextTime + rmi.getTimeLong() * 60000);
                riis.add(rii);
                i++;
                if (i >= repeat) {
                    rmi.setEndDate(getCurrentDate(nextTime));
                    break;
                }
                cal.add(Calendar.DAY_OF_YEAR, interval <= 0 ? 1 : interval * 7);
                nextTime = cal.getTimeInMillis();
            }
        }
    } else { //per month
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(startTime);
        int startDay = cal.get(Calendar.DAY_OF_MONTH);
        int monthDay = rmi.getMonthDay();
        if (monthDay > 0) {
            cal.add(Calendar.DAY_OF_YEAR, rmi.getMonthDay() - startDay);
            startTime = cal.getTimeInMillis();
            long currTime = System.currentTimeMillis();
            long nextTime = startTime;
            int interval = rmi.getMonthInterval();
            if (startTime < currTime) {
                cal.add(Calendar.MONTH, interval);
                nextTime = cal.getTimeInMillis();
            }
            rmi.setStartDate(getCurrentDate(nextTime));
            if (rmi.getEndType() == RecurringMeetingInfo.RECURRING_ENDTYPE_DATE) {
                long endDate = rmi.getEndDate();
                rmi.setEndDate(getCurrentDate(endDate));
                while (nextTime < endDate) {
                    RecurInstanceInfo rii = new RecurInstanceInfo();
                    rii.setStartTime(nextTime);
                    rii.setEndTime(nextTime + rmi.getTimeLong() * 60000);
                    riis.add(rii);
                    cal.add(Calendar.MONTH, interval);
                    nextTime = cal.getTimeInMillis();
                }
            } else {
                int repeat = rmi.getEndAfterNumber();
                int i = 0;
                while (i < repeat) {
                    RecurInstanceInfo rii = new RecurInstanceInfo();
                    rii.setStartTime(nextTime);
                    rii.setEndTime(nextTime + rmi.getTimeLong() * 60000);
                    riis.add(rii);
                    i++;
                    if (i >= repeat) {
                        rmi.setEndDate(getCurrentDate(nextTime));
                        break;
                    }
                    cal.add(Calendar.MONTH, interval);
                    nextTime = cal.getTimeInMillis();
                }
            }
        } else {
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.add(Calendar.MONTH, 1);
            cal.add(Calendar.DAY_OF_YEAR, monthDay);
            startTime = cal.getTimeInMillis();
            long currTime = System.currentTimeMillis();
            long nextTime = startTime;
            int interval = rmi.getMonthInterval();
            if (startTime < currTime) {
                cal.set(Calendar.DAY_OF_MONTH, 1);
                cal.add(Calendar.MONTH, interval + 1);
                cal.add(Calendar.DAY_OF_YEAR, monthDay);
                nextTime = cal.getTimeInMillis();
            }
            rmi.setStartDate(getCurrentDate(nextTime));
            if (rmi.getEndType() == RecurringMeetingInfo.RECURRING_ENDTYPE_DATE) {
                long endDate = rmi.getEndDate();
                rmi.setEndDate(getCurrentDate(endDate));
                while (nextTime < endDate) {
                    RecurInstanceInfo rii = new RecurInstanceInfo();
                    rii.setStartTime(nextTime);
                    rii.setEndTime(nextTime + rmi.getTimeLong() * 60000);
                    riis.add(rii);
                    cal.add(Calendar.MONTH, interval + 1);
                    cal.add(Calendar.DAY_OF_YEAR, monthDay);
                    nextTime = cal.getTimeInMillis();
                }
            } else {
                int repeat = rmi.getEndAfterNumber();
                int i = 0;
                while (i < repeat) {
                    RecurInstanceInfo rii = new RecurInstanceInfo();
                    rii.setStartTime(nextTime);
                    rii.setEndTime(nextTime + rmi.getTimeLong() * 60000);
                    riis.add(rii);
                    i++;
                    if (i >= repeat) {
                        rmi.setEndDate(getCurrentDate(nextTime));
                        break;
                    }
                    cal.set(Calendar.DAY_OF_MONTH, 1);
                    cal.add(Calendar.MONTH, interval + 1);
                    cal.add(Calendar.DAY_OF_YEAR, monthDay);
                    nextTime = cal.getTimeInMillis();
                }
            }
        }
    }
    //      ri.getRecurInstanceInfos().add(rii);
    return ri;
}

From source file:com.iisigroup.cap.log.TimeFolderSizeRollingFileAppender.java

public Date getNextCheckDate(Date now) {
    this.setTime(now);

    switch (type) {
    case TimeFolderSizeRollingFileAppender.TOP_OF_MINUTE:
        this.set(Calendar.SECOND, 0);
        this.set(Calendar.MILLISECOND, 0);
        this.add(Calendar.MINUTE, 1);
        break;/*from   w w  w .j  a  va2  s .  c o  m*/
    case TimeFolderSizeRollingFileAppender.TOP_OF_HOUR:
        this.set(Calendar.MINUTE, 0);
        this.set(Calendar.SECOND, 0);
        this.set(Calendar.MILLISECOND, 0);
        this.add(Calendar.HOUR_OF_DAY, 1);
        break;
    case TimeFolderSizeRollingFileAppender.HALF_DAY:
        this.set(Calendar.MINUTE, 0);
        this.set(Calendar.SECOND, 0);
        this.set(Calendar.MILLISECOND, 0);
        int hour = get(Calendar.HOUR_OF_DAY);
        if (hour < 12) {
            this.set(Calendar.HOUR_OF_DAY, 12);
        } else {
            this.set(Calendar.HOUR_OF_DAY, 0);
            this.add(Calendar.DAY_OF_MONTH, 1);
        }
        break;
    case TimeFolderSizeRollingFileAppender.TOP_OF_DAY:
        this.set(Calendar.HOUR_OF_DAY, 0);
        this.set(Calendar.MINUTE, 0);
        this.set(Calendar.SECOND, 0);
        this.set(Calendar.MILLISECOND, 0);
        this.add(Calendar.DATE, 1);
        break;
    case TimeFolderSizeRollingFileAppender.TOP_OF_WEEK:
        this.set(Calendar.DAY_OF_WEEK, getFirstDayOfWeek());
        this.set(Calendar.HOUR_OF_DAY, 0);
        this.set(Calendar.SECOND, 0);
        this.set(Calendar.MILLISECOND, 0);
        this.add(Calendar.WEEK_OF_YEAR, 1);
        break;
    case TimeFolderSizeRollingFileAppender.TOP_OF_MONTH:
        this.set(Calendar.DATE, 1);
        this.set(Calendar.HOUR_OF_DAY, 0);
        this.set(Calendar.SECOND, 0);
        this.set(Calendar.MILLISECOND, 0);
        this.add(Calendar.MONTH, 1);
        break;
    default:
        throw new IllegalStateException("Unknown periodicity type.");
    }
    return getTime();
}

From source file:cn.mljia.common.notify.utils.DateUtils.java

/**
 * ?//from w  ww  .  j a  v a2 s. co m
 * 
 * @param date
 * @return
 */
public static Date getWeekStart(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.get(Calendar.WEEK_OF_YEAR);
    int firstDay = calendar.getFirstDayOfWeek();
    calendar.set(Calendar.DAY_OF_WEEK, firstDay);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTime();
}