Example usage for java.util Calendar SUNDAY

List of usage examples for java.util Calendar SUNDAY

Introduction

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

Prototype

int SUNDAY

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

Click Source Link

Document

Value of the #DAY_OF_WEEK field indicating Sunday.

Usage

From source file:br.com.transport.report.ManagerReportBean.java

/**
 * Retorna o ultimo dia da semana//from  w  w  w.j  a va2 s .c  o m
 * @return
 */
private Calendar getLastCalendar() {

    Calendar end = GregorianCalendar.getInstance();
    end.setFirstDayOfWeek(Calendar.SUNDAY);
    end.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    end.set(Calendar.HOUR_OF_DAY, 23);
    end.set(Calendar.MINUTE, 59);
    end.set(Calendar.SECOND, 59);
    end.setTime(end.getTime());

    return end;
}

From source file:org.betaconceptframework.astroboa.portal.utility.CalendarUtils.java

public Calendar[] getCurrentWeekendPeriod(TimeZone zone, Locale locale) {
    Calendar weekendStart = GregorianCalendar.getInstance(zone, locale);
    Calendar weekendEnd = GregorianCalendar.getInstance(zone, locale);

    // we will include Friday in the weekend
    int currentDayOfWeek = weekendStart.get(Calendar.DAY_OF_WEEK);

    if (currentDayOfWeek == Calendar.FRIDAY) {
        // set to SUNDAY
        weekendEnd.add(Calendar.DAY_OF_MONTH, 2);
    } else if (currentDayOfWeek == Calendar.SATURDAY) {
        // set to SUNDAY
        weekendEnd.add(Calendar.DAY_OF_MONTH, 1);
    } else if (currentDayOfWeek == Calendar.SUNDAY) {
        //Do nothing
    } else {// w  ww.j  a  v  a 2  s . c  o  m
        while (weekendStart.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY) {
            weekendStart.add(Calendar.DAY_OF_MONTH, 1);
            weekendEnd.add(Calendar.DAY_OF_MONTH, 1);
        }

        // now weekendStart and weekendEnd is FRIDAY and we 
        // set weekendEnd to SUNDAY
        weekendEnd.add(Calendar.DAY_OF_MONTH, 2);

    }

    setCalendarToStartOfDay(weekendStart);
    setCalendarToEndOfDay(weekendEnd);

    Calendar[] period = { weekendStart, weekendEnd };
    return period;
}

From source file:com.adobe.acs.commons.http.headers.impl.WeeklyExpiresHeaderFilterTest.java

@Test(expected = ConfigurationException.class)
public void testDoActivateInvalidLowDayOfWeek() throws Exception {
    properties.put(WeeklyExpiresHeaderFilter.PROP_EXPIRES_DAY_OF_WEEK, Calendar.SUNDAY - 1);
    filter.doActivate(componentContext);
}

From source file:DateUtils.java

public static final String getDayString(int day) {
    switch (day) {
    case Calendar.SUNDAY:
        return "SUNDAY";
    case Calendar.MONDAY:
        return "MONDAY";
    case Calendar.TUESDAY:
        return "TUESDAY";
    case Calendar.WEDNESDAY:
        return "WEDNESDAY";
    case Calendar.THURSDAY:
        return "THURSDAY";
    case Calendar.FRIDAY:
        return "FRIDAY";
    case Calendar.SATURDAY:
        return "SATURDAY";
    }//  ww w .  j a  v  a  2s.c  o  m
    return "";
}

From source file:Main.java

/**
 * This constructs an <code>Iterator</code> over each day in a date
 * range defined by a focus date and range style.
 *
 * For instance, passing Thursday, July 4, 2002 and a
 * <code>RANGE_MONTH_SUNDAY</code> will return an <code>Iterator</code>
 * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3,
 * 2002, returning a Calendar instance for each intermediate day.
 *
 * This method provides an iterator that returns Calendar objects.
 * The days are progressed using {@link Calendar#add(int, int)}.
 *
 * @param focus  the date to work with//from w ww .  jav  a  2 s. c  om
 * @param rangeStyle  the style constant to use. Must be one of
 * {@link DateUtils#RANGE_MONTH_SUNDAY}, 
 * {@link DateUtils#RANGE_MONTH_MONDAY},
 * {@link DateUtils#RANGE_WEEK_SUNDAY},
 * {@link DateUtils#RANGE_WEEK_MONDAY},
 * {@link DateUtils#RANGE_WEEK_RELATIVE},
 * {@link DateUtils#RANGE_WEEK_CENTER}
 * @return the date iterator
 * @throws IllegalArgumentException if the date is <code>null</code>
 * @throws IllegalArgumentException if the rangeStyle is invalid
 */
public static Iterator iterator(Calendar focus, int rangeStyle) {
    if (focus == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    Calendar start = null;
    Calendar end = null;
    int startCutoff = Calendar.SUNDAY;
    int endCutoff = Calendar.SATURDAY;
    switch (rangeStyle) {
    case RANGE_MONTH_SUNDAY:
    case RANGE_MONTH_MONDAY:
        //Set start to the first of the month
        start = truncate(focus, Calendar.MONTH);
        //Set end to the last of the month
        end = (Calendar) start.clone();
        end.add(Calendar.MONTH, 1);
        end.add(Calendar.DATE, -1);
        //Loop start back to the previous sunday or monday
        if (rangeStyle == RANGE_MONTH_MONDAY) {
            startCutoff = Calendar.MONDAY;
            endCutoff = Calendar.SUNDAY;
        }
        break;
    case RANGE_WEEK_SUNDAY:
    case RANGE_WEEK_MONDAY:
    case RANGE_WEEK_RELATIVE:
    case RANGE_WEEK_CENTER:
        //Set start and end to the current date
        start = truncate(focus, Calendar.DATE);
        end = truncate(focus, Calendar.DATE);
        switch (rangeStyle) {
        case RANGE_WEEK_SUNDAY:
            //already set by default
            break;
        case RANGE_WEEK_MONDAY:
            startCutoff = Calendar.MONDAY;
            endCutoff = Calendar.SUNDAY;
            break;
        case RANGE_WEEK_RELATIVE:
            startCutoff = focus.get(Calendar.DAY_OF_WEEK);
            endCutoff = startCutoff - 1;
            break;
        case RANGE_WEEK_CENTER:
            startCutoff = focus.get(Calendar.DAY_OF_WEEK) - 3;
            endCutoff = focus.get(Calendar.DAY_OF_WEEK) + 3;
            break;
        }
        break;
    default:
        throw new IllegalArgumentException("The range style " + rangeStyle + " is not valid.");
    }
    if (startCutoff < Calendar.SUNDAY) {
        startCutoff += 7;
    }
    if (startCutoff > Calendar.SATURDAY) {
        startCutoff -= 7;
    }
    if (endCutoff < Calendar.SUNDAY) {
        endCutoff += 7;
    }
    if (endCutoff > Calendar.SATURDAY) {
        endCutoff -= 7;
    }
    while (start.get(Calendar.DAY_OF_WEEK) != startCutoff) {
        start.add(Calendar.DATE, -1);
    }
    while (end.get(Calendar.DAY_OF_WEEK) != endCutoff) {
        end.add(Calendar.DATE, 1);
    }
    return new DateIterator(start, end);
}

From source file:org.apache.lens.cube.parse.DateUtil.java

public static Date resolveRelativeDate(String str, Date now) throws SemanticException {
    if (StringUtils.isBlank(str)) {
        throw new SemanticException(ErrorMsg.NULL_DATE_VALUE);
    }/*from   ww w.  j a  v a 2 s. co  m*/

    // Resolve NOW with proper granularity
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(now);

    str = str.toLowerCase();
    Matcher relativeMatcher = P_RELATIVE.matcher(str);
    if (relativeMatcher.find()) {
        String nowWithGranularity = relativeMatcher.group();
        nowWithGranularity = nowWithGranularity.replaceAll("now", "");
        nowWithGranularity = nowWithGranularity.replaceAll("\\.", "");

        Matcher granularityMatcher = P_UNIT.matcher(nowWithGranularity);
        if (granularityMatcher.find()) {
            String unit = granularityMatcher.group().toLowerCase();
            if ("year".equals(unit)) {
                calendar = DateUtils.truncate(calendar, YEAR);
            } else if ("month".equals(unit)) {
                calendar = DateUtils.truncate(calendar, MONTH);
            } else if ("week".equals(unit)) {
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                calendar = DateUtils.truncate(calendar, DAY_OF_MONTH);
            } else if ("day".equals(unit)) {
                calendar = DateUtils.truncate(calendar, DAY_OF_MONTH);
            } else if ("hour".equals(unit)) {
                calendar = DateUtils.truncate(calendar, Calendar.HOUR_OF_DAY);
            } else if ("minute".equals(unit)) {
                calendar = DateUtils.truncate(calendar, Calendar.MINUTE);
            } else if ("second".equals(unit)) {
                calendar = DateUtils.truncate(calendar, Calendar.SECOND);
            } else {
                throw new SemanticException(ErrorMsg.INVALID_TIME_UNIT, unit);
            }
        }
    }

    // Get rid of 'now' part and whitespace
    String diffStr = str.replaceAll(RELATIVE, "").replace(WSPACE, "");
    TimeDiff diff = TimeDiff.parseFrom(diffStr);
    return diff.offsetFrom(calendar.getTime());
}

From source file:de.fhbingen.wbs.wpOverview.tabs.AvailabilityGraph.java

/**
 * Set the view to WEEK./* w w  w  . ja v  a2 s .  c  om*/
 */
private void setWeekView() {

    actualStart = (GregorianCalendar) actualDay.clone();
    actualStart.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    actualStart.set(Calendar.HOUR_OF_DAY, 0);
    actualStart.set(Calendar.MINUTE, 0);

    actualEnd = (GregorianCalendar) actualDay.clone();
    actualEnd.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    actualEnd.set(Calendar.HOUR_OF_DAY, 23);
    actualEnd.set(Calendar.MINUTE, 59);

    actualStart.add(Calendar.HOUR_OF_DAY, -1);
    actualEnd.add(Calendar.HOUR_OF_DAY, 1);

    actualView = WEEK;

    GregorianCalendar helper = (GregorianCalendar) actualEnd.clone();
    helper.add(Calendar.DATE, -1);

    makeChart(LocalizedStrings.getGeneralStrings().calendarWeekAbbreviation() + " "
            + new SimpleDateFormat("ww yyyy").format(helper.getTime()));
}

From source file:com.philliphsu.clock2.alarms.Alarm.java

public long ringsAt() {
    // Always with respect to the current date and time
    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, hour());
    calendar.set(Calendar.MINUTE, minutes());
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    long baseRingTime = calendar.getTimeInMillis();

    if (!hasRecurrence()) {
        if (baseRingTime <= System.currentTimeMillis()) {
            // The specified time has passed for today
            baseRingTime += TimeUnit.DAYS.toMillis(1);
        }/*  ww w.  j av a  2s. c o m*/
        return baseRingTime;
    } else {
        // Compute the ring time just for the next closest recurring day.
        // Remember that day constants defined in the Calendar class are
        // not zero-based like ours, so we have to compensate with an offset
        // of magnitude one, with the appropriate sign based on the situation.
        int weekdayToday = calendar.get(Calendar.DAY_OF_WEEK);
        int numDaysFromToday = -1;

        for (int i = weekdayToday; i <= Calendar.SATURDAY; i++) {
            if (isRecurring(i - 1 /*match up with our day constant*/)) {
                if (i == weekdayToday) {
                    if (baseRingTime > System.currentTimeMillis()) {
                        // The normal ring time has not passed yet
                        numDaysFromToday = 0;
                        break;
                    }
                } else {
                    numDaysFromToday = i - weekdayToday;
                    break;
                }
            }
        }

        // Not computed yet
        if (numDaysFromToday < 0) {
            for (int i = Calendar.SUNDAY; i < weekdayToday; i++) {
                if (isRecurring(i - 1 /*match up with our day constant*/)) {
                    numDaysFromToday = Calendar.SATURDAY - weekdayToday + i;
                    break;
                }
            }
        }

        // Still not computed yet. The only recurring day is weekdayToday,
        // and its normal ring time has already passed.
        if (numDaysFromToday < 0 && isRecurring(weekdayToday - 1)
                && baseRingTime <= System.currentTimeMillis()) {
            numDaysFromToday = 7;
        }

        if (numDaysFromToday < 0)
            throw new IllegalStateException("How did we get here?");

        return baseRingTime + TimeUnit.DAYS.toMillis(numDaysFromToday);
    }
}

From source file:org.jasig.schedassist.model.AvailableBlockBuilderTest.java

/**
 * Every week day from June 24 2008 to August 6 2008.
 * //from  w  w  w.j av a 2  s  . c  om
 * @throws Exception
 */
@Test
public void testCreateBlocksExample2() throws Exception {
    SimpleDateFormat dateFormat = CommonDateOperations.getDateFormat();
    Date startDate = dateFormat.parse("20080624");
    Date endDate = dateFormat.parse("20080806");
    Set<AvailableBlock> blocks = AvailableBlockBuilder.createBlocks("9:00 AM", "11:30 AM", "MTWRF", startDate,
            endDate);
    // 32 weekdays between June 24 2008 and August 6 2008 (including Aug 6 2008)
    assertEquals(32, blocks.size());
    for (AvailableBlock block : blocks) {
        assertEquals(150, block.getDurationInMinutes());
        Calendar cal = Calendar.getInstance();
        cal.setTime(block.getStartTime());
        assertEquals(9, cal.get(Calendar.HOUR_OF_DAY));
        assertEquals(0, cal.get(Calendar.MINUTE));
        cal.setTime(block.getEndTime());
        assertEquals(11, cal.get(Calendar.HOUR_OF_DAY));
        assertEquals(30, cal.get(Calendar.MINUTE));
        assertNotSame(Calendar.SATURDAY, cal.get(Calendar.DAY_OF_WEEK));
        assertNotSame(Calendar.SUNDAY, cal.get(Calendar.DAY_OF_WEEK));
        assertEquals(1, block.getVisitorLimit());
    }

    blocks = AvailableBlockBuilder.createBlocks("9:00 AM", "11:30 AM", "MTWRF", startDate, endDate, 64);
    // 32 weekdays between June 24 2008 and August 6 2008 (including Aug 6 2008)
    assertEquals(32, blocks.size());
    for (AvailableBlock block : blocks) {
        assertEquals(150, block.getDurationInMinutes());
        Calendar cal = Calendar.getInstance();
        cal.setTime(block.getStartTime());
        assertEquals(9, cal.get(Calendar.HOUR_OF_DAY));
        assertEquals(0, cal.get(Calendar.MINUTE));
        cal.setTime(block.getEndTime());
        assertEquals(11, cal.get(Calendar.HOUR_OF_DAY));
        assertEquals(30, cal.get(Calendar.MINUTE));
        assertNotSame(Calendar.SATURDAY, cal.get(Calendar.DAY_OF_WEEK));
        assertNotSame(Calendar.SUNDAY, cal.get(Calendar.DAY_OF_WEEK));
        assertEquals(64, block.getVisitorLimit());
    }
}

From source file:net.granoeste.commons.util.DateUtils.java

/**
 * ???//from w ww  .  j a  v a2s.c  o  m
 *
 * @param cal
 * @param amount ex) last week : -1. the current week : 0. the next week : 1
 */
public static void shiftDateOnMondayOfTheWeek(final Calendar cal, final int amount) {

    cal.add(Calendar.WEEK_OF_YEAR, amount);
    if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        cal.add(Calendar.DATE, -6);
    } else {
        cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK) + 2);
    }
}