Example usage for org.apache.commons.lang.time DateUtils addMinutes

List of usage examples for org.apache.commons.lang.time DateUtils addMinutes

Introduction

In this page you can find the example usage for org.apache.commons.lang.time DateUtils addMinutes.

Prototype

public static Date addMinutes(Date date, int amount) 

Source Link

Document

Adds a number of minutes to a date returning a new object.

Usage

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

/**
 * Creates a minimum size {@link AvailableBlock} using the argument endTime as the end and MINIMUM_MINUTES minutes
 * prior to endTime as the start.//w  w  w  .jav  a2 s  .  c o m
 * 
 * @param endTime
 * @return the new block
 */
public static AvailableBlock createMinimumEndBlock(final Date endTime) {
    Date startTime = DateUtils.addMinutes(endTime, -MINIMUM_MINUTES);
    return createBlock(startTime, endTime);
}

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

/**
 * Validate IllegalArgumentException thrown for all invalid arguments to createBlock.
 * @throws Exception/*w w w  .  j  av  a  2  s.c  o m*/
 */
@Test
public void testCreateBlockInvalid() throws Exception {
    Date nullDate = null;
    try {
        AvailableBlockBuilder.createBlock(nullDate, nullDate, 1);
        fail("expected IllegalArgumentException not thrown for null startTime");
    } catch (IllegalArgumentException e) {
        //success
    }

    try {
        AvailableBlockBuilder.createBlock(new Date(), nullDate, 1);
        fail("expected IllegalArgumentException not thrown for null endTime");
    } catch (IllegalArgumentException e) {
        //success
    }

    Date now = new Date();
    try {
        AvailableBlockBuilder.createBlock(now, now, 1);
        fail("expected IllegalArgumentException not thrown for equivalent start/endtime");
    } catch (IllegalArgumentException e) {
        //success
    }

    try {
        AvailableBlockBuilder.createBlock(now, DateUtils.addMinutes(now, 1), 0);
        fail("expected IllegalArgumentException not thrown for visitorLimit less than 1");
    } catch (IllegalArgumentException e) {
        //success
    }
}

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

/**
 * Expand an {@link AvailableBlock} that was created with 9:00 AM and 3:00 PM as
 * start and end times, respectively.//from www .  j ava  2 s.  c  o m
 * 
 * @throws Exception
 */
@Test
public void testExpand() throws Exception {
    SimpleDateFormat dateTimeFormat = CommonDateOperations.getDateTimeFormat();
    Date startTime = dateTimeFormat.parse("20080601-0900");
    Date endTime = dateTimeFormat.parse("20080601-1500");

    AvailableBlock original = AvailableBlockBuilder.createBlock(startTime, endTime);
    assertNotNull(original);
    Set<AvailableBlock> expanded = AvailableBlockBuilder.expand(original, 30);
    assertEquals(12, expanded.size());

    Date currentStart = startTime;
    for (AvailableBlock block : expanded) {
        assertEquals(30, block.getDurationInMinutes());
        assertEquals(currentStart, block.getStartTime());
        currentStart = DateUtils.addMinutes(currentStart, 30);
        assertEquals(currentStart, block.getEndTime());
    }
}

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

/**
 * Assert expand function preserves original visitorLimit.
 * /*from w w w . ja  v a  2  s  .c  o  m*/
 * @throws Exception
 */
@Test
public void testExpandPreserveVisitorLimit() throws Exception {
    SimpleDateFormat dateTimeFormat = CommonDateOperations.getDateTimeFormat();
    Date startTime = dateTimeFormat.parse("20080601-0900");
    Date endTime = dateTimeFormat.parse("20080601-1500");

    AvailableBlock toExpand = AvailableBlockBuilder.createBlock(startTime, endTime, 10);

    SortedSet<AvailableBlock> expanded20 = AvailableBlockBuilder.expand(toExpand, 20);
    assertEquals(18, expanded20.size());
    Date currentStart = startTime;
    for (AvailableBlock block : expanded20) {
        assertEquals(20, block.getDurationInMinutes());
        assertEquals(10, block.getVisitorLimit());
        assertEquals(currentStart, block.getStartTime());
        currentStart = DateUtils.addMinutes(currentStart, 20);
        assertEquals(currentStart, block.getEndTime());
    }

    SortedSet<AvailableBlock> expanded40 = AvailableBlockBuilder.expand(toExpand, 40);
    assertEquals(9, expanded40.size());
    currentStart = startTime;
    for (AvailableBlock block : expanded40) {
        assertEquals(40, block.getDurationInMinutes());
        assertEquals(10, block.getVisitorLimit());
        assertEquals(currentStart, block.getStartTime());
        currentStart = DateUtils.addMinutes(currentStart, 40);
        assertEquals(currentStart, block.getEndTime());
    }

    SortedSet<AvailableBlock> expanded60 = AvailableBlockBuilder.expand(toExpand, 60);
    assertEquals(6, expanded60.size());
    currentStart = startTime;
    for (AvailableBlock block : expanded60) {
        assertEquals(60, block.getDurationInMinutes());
        assertEquals(10, block.getVisitorLimit());
        assertEquals(currentStart, block.getStartTime());
        currentStart = DateUtils.addMinutes(currentStart, 60);
        assertEquals(currentStart, block.getEndTime());
    }
}

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

/**
 * Test the overloaded createSmallestAllowedBlock methods.
 * //w  ww .  j  a  v  a 2 s .c o m
 * @throws Exception
 */
@Test
public void testCreateSmallestAllowedBlock() throws Exception {
    Date date = CommonDateOperations.parseDateTimePhrase("20091103-1500");
    AvailableBlock block = AvailableBlockBuilder.createSmallestAllowedBlock(date);
    assertEquals(date, block.getStartTime());
    assertEquals(DateUtils.addMinutes(date, AvailableBlockBuilder.MINIMUM_MINUTES), block.getEndTime());
    assertEquals(1, block.getVisitorLimit());

    block = AvailableBlockBuilder.createSmallestAllowedBlock(date, 10);
    assertEquals(date, block.getStartTime());
    assertEquals(DateUtils.addMinutes(date, AvailableBlockBuilder.MINIMUM_MINUTES), block.getEndTime());
    assertEquals(10, block.getVisitorLimit());

    block = AvailableBlockBuilder.createSmallestAllowedBlock("20091103-1500");
    assertEquals(date, block.getStartTime());
    assertEquals(DateUtils.addMinutes(date, AvailableBlockBuilder.MINIMUM_MINUTES), block.getEndTime());
    assertEquals(1, block.getVisitorLimit());
}

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

/**
 * @throws ParseException /*  w ww. j  a  v a2 s .  c o m*/
 * @throws InputFormatException 
 * 
 */
@Test
public void testTwentyMinuteDuration() throws InputFormatException, ParseException {
    SimpleDateFormat dateFormat = CommonDateOperations.getDateFormat();
    SortedSet<AvailableBlock> blocks = AvailableBlockBuilder.createBlocks("9:00 AM", "11:40 AM", "MW",
            dateFormat.parse("20100830"), dateFormat.parse("20100903"));
    Assert.assertEquals(2, blocks.size());
    Assert.assertEquals(CommonDateOperations.getDateTimeFormat().parse("20100830-0900"),
            blocks.first().getStartTime());
    Assert.assertEquals(CommonDateOperations.getDateTimeFormat().parse("20100830-1140"),
            blocks.first().getEndTime());

    Assert.assertEquals(CommonDateOperations.getDateTimeFormat().parse("20100901-0900"),
            blocks.last().getStartTime());
    Assert.assertEquals(CommonDateOperations.getDateTimeFormat().parse("20100901-1140"),
            blocks.last().getEndTime());

    SortedSet<AvailableBlock> expanded = AvailableBlockBuilder.expand(blocks, 20);
    Assert.assertEquals(16, expanded.size());

    Date originalStart = CommonDateOperations.getDateTimeFormat().parse("20100830-0900");

    Date currentStart = originalStart;
    for (AvailableBlock e : expanded) {
        if (!DateUtils.isSameDay(e.getStartTime(), currentStart)) {
            currentStart = DateUtils.addDays(originalStart, 2);
        }
        Assert.assertEquals(currentStart, e.getStartTime());
        currentStart = DateUtils.addMinutes(currentStart, 20);
        Assert.assertEquals(currentStart, e.getEndTime());
    }
}

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

/**
 * Returns the set of {@link AvailableBlock} objects within this instance
 * that conflict with the argument.//  w ww.ja  v  a  2 s  .  c o  m
 * 
 * A conflict is defined as any overlap of 1 minute or more.
 * 
 * @param conflict
 * @return a set of conflicting blocks within this instance that conflict with the block argument
 */
protected Set<AvailableBlock> locateConflicting(final AvailableBlock conflict) {
    Set<AvailableBlock> conflictingKeys = new HashSet<AvailableBlock>();

    Date conflictDayStart = DateUtils.truncate(conflict.getStartTime(), java.util.Calendar.DATE);
    Date conflictDayEnd = DateUtils.addDays(DateUtils.truncate(conflict.getEndTime(), java.util.Calendar.DATE),
            1);
    conflictDayEnd = DateUtils.addMinutes(conflictDayEnd, -1);

    AvailableBlock rangeStart = AvailableBlockBuilder.createPreferredMinimumDurationBlock(conflictDayStart,
            meetingDurations);
    LOG.debug("rangeStart: " + rangeStart);
    AvailableBlock rangeEnd = AvailableBlockBuilder.createBlockEndsAt(conflictDayEnd,
            meetingDurations.getMinLength());
    LOG.debug("rangeEnd: " + rangeStart);

    SortedMap<AvailableBlock, AvailableStatus> subMap = blockMap.subMap(rangeStart, rangeEnd);
    LOG.debug("subset of blockMap size: " + subMap.size());

    for (AvailableBlock mapKey : subMap.keySet()) {
        // all the AvailableBlock keys in the map have start/endtimes truncated to the minute
        // shift the key slightly forward (10 seconds) so that conflicts that start or end on the
        // same minute as a key does don't result in false positives
        Date minuteWithinBlock = DateUtils.addSeconds(mapKey.getStartTime(), 10);
        boolean shortCircuit = true;
        while (shortCircuit && CommonDateOperations.equalsOrBefore(minuteWithinBlock, mapKey.getEndTime())) {
            if (minuteWithinBlock.before(conflict.getEndTime())
                    && minuteWithinBlock.after(conflict.getStartTime())) {
                conflictingKeys.add(mapKey);
                shortCircuit = false;
            }
            minuteWithinBlock = DateUtils.addMinutes(minuteWithinBlock, 1);
        }
    }

    return conflictingKeys;
}

From source file:org.jasig.schedassist.portlet.VisibleScheduleTag.java

@Override
public int doStartTagInternal() {
    RenderRequest renderRequest = (RenderRequest) pageContext.getRequest().getAttribute(PORTLET_REQUEST);
    RenderResponse renderResponse = (RenderResponse) pageContext.getRequest().getAttribute(PORTLET_RESPONSE);

    final Date scheduleStart = visibleSchedule.getScheduleStart();
    if (null == scheduleStart) {
        // the visibleSchedule is empty, short circuit
        try {/*w  w  w. java  2s  .co m*/
            StringBuilder noappointments = new StringBuilder();
            noappointments.append("<span class=\"none-available\">");
            noappointments.append(getMessageSource().getMessage("no.available.appointments", null, null));
            noappointments.append("</span>");
            pageContext.getOut().write(noappointments.toString());
        } catch (IOException e) {
            LOG.error("IOException occurred in doStartTag", e);
        }
        // SKIP_BODY means don't print any content from body of tag
        return SKIP_BODY;
    }

    LOG.debug("scheduleStart: " + scheduleStart);
    SortedMap<Date, List<AvailableBlock>> dailySchedules = new TreeMap<Date, List<AvailableBlock>>();
    Date index = DateUtils.truncate(scheduleStart, java.util.Calendar.DATE);
    Date scheduleEnd = visibleSchedule.getScheduleEnd();
    while (index.before(scheduleEnd)) {
        dailySchedules.put(index, new ArrayList<AvailableBlock>());
        index = DateUtils.addDays(index, 1);
    }
    final Date lastMapKey = dailySchedules.lastKey();
    LOG.debug("visibleSchedule spans " + dailySchedules.keySet().size() + " days");

    try {
        SortedMap<AvailableBlock, AvailableStatus> scheduleBlockMap = visibleSchedule.getBlockMap();
        int numberOfEventsToDisplay = 0;
        for (AvailableBlock block : scheduleBlockMap.keySet()) {
            Date eventStartDate = block.getStartTime();
            LOG.debug("event start date: " + eventStartDate);
            Date mapKey = DateUtils.truncate(eventStartDate, java.util.Calendar.DATE);
            if (CommonDateOperations.equalsOrAfter(eventStartDate, scheduleStart)
                    && dailySchedules.containsKey(mapKey)) {
                dailySchedules.get(mapKey).add(block);
                numberOfEventsToDisplay++;
            }
        }
        LOG.debug("number of events to display: " + numberOfEventsToDisplay);
        if (numberOfEventsToDisplay == 0) {
            // no available times in this range!
            StringBuilder noappointments = new StringBuilder();
            noappointments.append("<span class=\"none-available\">");
            noappointments.append(getMessageSource().getMessage("no.available.appointments", null, null));
            noappointments.append("</span>");
            pageContext.getOut().write(noappointments.toString());
        } else {
            int weekNumber = 1;
            Date currentWeekStart = DateUtils.truncate(scheduleStart, java.util.Calendar.DATE);
            Date currentWeekFinish = DateUtils.addDays(currentWeekStart,
                    CommonDateOperations.numberOfDaysUntilSunday(currentWeekStart));
            currentWeekFinish = DateUtils.addMinutes(currentWeekFinish, -1);

            boolean renderAnotherWeek = true;

            while (renderAnotherWeek) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("will render another week using currentWeekStart " + currentWeekStart
                            + " and currentWeekFinish " + currentWeekFinish);
                }
                SortedMap<Date, List<AvailableBlock>> subMap = dailySchedules.subMap(currentWeekStart,
                        currentWeekFinish);
                renderWeek(pageContext.getOut(), weekNumber++, subMap, scheduleBlockMap, renderRequest,
                        renderResponse);

                currentWeekStart = DateUtils.addMinutes(currentWeekFinish, 1);
                currentWeekFinish = DateUtils.addDays(currentWeekStart, 7);
                currentWeekFinish = DateUtils.addMinutes(currentWeekFinish, -1);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("recalculated currentWeekStart " + currentWeekStart + ", currentWeekFinish "
                            + currentWeekFinish);
                }

                if (currentWeekStart.after(lastMapKey)) {
                    renderAnotherWeek = false;
                    LOG.debug("will not render another week");
                }
            }
        }

    } catch (IOException e) {
        LOG.error("IOException occurred in doStartTag", e);
    }
    // SKIP_BODY means don't print any content from body of tag
    return SKIP_BODY;
}

From source file:org.jasig.schedassist.portlet.webflow.FlowHelperTest.java

@Test
public void testValidateChosenStartTime() throws InputFormatException {
    FlowHelper flowHelper = new FlowHelper();

    VisibleWindow window = VisibleWindow.fromKey("1,1");
    Date now = new Date();
    Assert.assertEquals(FlowHelper.NO, flowHelper.validateChosenStartTime(window, now));
    // make sure it doesn't work in the past either
    Assert.assertEquals(FlowHelper.NO,//ww  w . j  av a 2 s  . com
            flowHelper.validateChosenStartTime(window, DateUtils.addHours(new Date(), -1)));

    Assert.assertEquals(FlowHelper.YES,
            flowHelper.validateChosenStartTime(window, DateUtils.addHours(new Date(), 2)));
    Assert.assertEquals(FlowHelper.YES, flowHelper.validateChosenStartTime(window,
            DateUtils.addHours(window.calculateCurrentWindowEnd(), -1)));

    // still good 1 minute before window end
    Assert.assertEquals(FlowHelper.YES, flowHelper.validateChosenStartTime(window,
            DateUtils.addMinutes(window.calculateCurrentWindowEnd(), -1)));

    Assert.assertEquals(FlowHelper.NO,
            flowHelper.validateChosenStartTime(window, window.calculateCurrentWindowEnd()));
    Assert.assertEquals(FlowHelper.NO, flowHelper.validateChosenStartTime(window,
            DateUtils.addHours(window.calculateCurrentWindowEnd(), 1)));

}

From source file:org.jasig.schedassist.web.owner.schedule.AvailableScheduleDataController.java

/**
 * //from   w  w w. j av a  2s.c o  m
 * @param date
 * @return
 */
public static Date roundDownToNearest15(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    int minutesField = cal.get(Calendar.MINUTE);
    int toRemove = minutesField % 15;

    Date result = date;
    if (toRemove != 0) {
        result = DateUtils.addMinutes(result, -toRemove);
    }

    return result;
}