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

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

Introduction

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

Prototype

public static Date truncate(Object date, int field) 

Source Link

Document

Truncate this date, leaving the field specified as the most significant field.

For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:00:00.000.

Usage

From source file:org.jasig.schedassist.impl.owner.SpringJDBCAvailableScheduleDaoImpl.java

@Override
public AvailableBlock retrieveTargetBlock(final IScheduleOwner owner, final Date startDate) {
    SortedSet<AvailableBlock> expanded = retrieveBlocksForDayInternal(owner, startDate);
    // truncate startDate to the second
    final Date truncatedStart = DateUtils.truncate(startDate, Calendar.MINUTE);

    if (expanded.size() > 0) {
        for (Iterator<AvailableBlock> expandedIterator = expanded.iterator(); expandedIterator.hasNext();) {
            AvailableBlock block = expandedIterator.next();
            if (block.getStartTime().equals(truncatedStart)) {
                // always return preferred minimum length block
                return block;
            }/*w ww  .j  a v a 2  s.  c  om*/
            if (block.getStartTime().after(truncatedStart)) {
                // iterated past the target block, can short circuit and say we didn't find it
                return null;
            }
        }
    }
    // block not found, return null
    return null;
}

From source file:org.jasig.schedassist.impl.owner.SpringJDBCAvailableScheduleDaoImpl.java

@Override
public AvailableBlock retrieveTargetBlock(IScheduleOwner owner, Date startDate, Date endDate) {
    SortedSet<AvailableBlock> expanded = retrieveBlocksForDayInternal(owner, startDate);
    // truncate startDate and endDate to the second
    final Date truncatedStart = DateUtils.truncate(startDate, Calendar.MINUTE);
    final Date truncatedEnd = DateUtils.truncate(endDate, Calendar.MINUTE);

    if (expanded.size() > 0) {
        for (Iterator<AvailableBlock> expandedIterator = expanded.iterator(); expandedIterator.hasNext();) {
            AvailableBlock block = expandedIterator.next();
            if (block.getStartTime().equals(truncatedStart)) {
                if (block.getEndTime().equals(truncatedEnd)) {
                    return block;
                }/* w w w  .j  av  a2s  .c o  m*/
                // start time matches but end time doesn't
                // check to see if the next block has matching end
                if (expandedIterator.hasNext()) {
                    AvailableBlock nextBlock = expandedIterator.next();
                    if (nextBlock.getEndTime().equals(truncatedEnd)) {
                        // start and end represent a double length block, combine and return
                        AvailableBlock combined = AvailableBlockBuilder.createBlock(block.getStartTime(),
                                nextBlock.getEndTime(), block.getVisitorLimit(), block.getMeetingLocation());
                        return combined;
                    }
                }
            }

            if (block.getStartTime().after(truncatedStart)) {
                // iterated past the target block, can short circuit and say we didn't find it
                return null;
            }
        }
    }
    // block not found, return null
    return null;
}

From source file:org.jasig.schedassist.impl.owner.SpringJDBCAvailableScheduleDaoImpl.java

@Override
public AvailableBlock retrieveTargetDoubleLengthBlock(IScheduleOwner owner, Date startDate) {
    SortedSet<AvailableBlock> expanded = retrieveBlocksForDayInternal(owner, startDate);
    // truncate startDate to the second
    final Date truncatedStart = DateUtils.truncate(startDate, Calendar.MINUTE);

    if (expanded.size() > 0) {
        for (Iterator<AvailableBlock> expandedIterator = expanded.iterator(); expandedIterator.hasNext();) {
            AvailableBlock block = expandedIterator.next();
            if (block.getStartTime().equals(truncatedStart)) {
                if (owner.getPreferredMeetingDurations().isDoubleLength() && expandedIterator.hasNext()) {
                    // combine the block with the next
                    AvailableBlock nextBlock = expandedIterator.next();
                    AvailableBlock combined = AvailableBlockBuilder.createBlock(block.getStartTime(),
                            nextBlock.getEndTime(), block.getVisitorLimit(), block.getMeetingLocation());
                    return combined;
                }//from ww  w  . java2 s . co  m
            }

            if (block.getStartTime().after(truncatedStart)) {
                // iterated past the target block, can short circuit and say we didn't find it
                return null;
            }
        }
    }
    // block not found, return null
    return null;
}

From source file:org.jasig.schedassist.impl.owner.SpringJDBCAvailableScheduleDaoImpl.java

/**
 * {@link AvailableBlock}s are combined before storage. The purpose of this method is to provide a consistent mechanism
 * for retrieving the block data for a specific day and expand them to the owner's preferred min duration.
 * /*from w w w.  ja  va 2 s. c om*/
 * @param owner
 * @param referenceDay
 * @return a possibly empty but never null {@link SortedSet} of {@link AvailableBlock}s with minimum preferred duration for the specified owner and calendar day.
 */
protected SortedSet<AvailableBlock> retrieveBlocksForDayInternal(IScheduleOwner owner, Date referenceDay) {
    // retrieve all blocks for the day.
    Date startOfDay = DateUtils.truncate(referenceDay, Calendar.DATE);
    Date endOfDay = DateUtils.addDays(startOfDay, 1);
    List<PersistenceAvailableBlock> scheduleRows = this.simpleJdbcTemplate.query(
            "select * from schedules where owner_id = ? and start_time >= ? and end_time < ?",
            new PersistenceAvailableBlockRowMapper(), owner.getId(), startOfDay, endOfDay);
    SortedSet<AvailableBlock> availableBlocks = new TreeSet<AvailableBlock>();
    for (PersistenceAvailableBlock row : scheduleRows) {
        availableBlocks.add(AvailableBlockBuilder.createBlock(row.getStartTime(), row.getEndTime(),
                row.getVisitorLimit(), row.getMeetingLocation()));
    }

    int ownerPreferredMinDuration = owner.getPreferredMeetingDurations().getMinLength();
    SortedSet<AvailableBlock> expanded = AvailableBlockBuilder.expand(availableBlocks,
            ownerPreferredMinDuration);
    return expanded;
}

From source file:org.jasig.schedassist.impl.owner.SpringJDBCAvailableScheduleDaoImpl.java

/**
 * Remove blocks from the schedules table from all owners that have endTimes prior
 * to "<daysPrior argument> before today".
 * /*from w w  w . ja  v a  2s .  co  m*/
 * @return the number of blocks removed by this operation
 */
@Transactional
@Override
public int purgeExpiredBlocks(final Integer daysPrior) {
    final String propertyValue = System.getProperty("org.jasig.schedassist.runScheduledTasks", "true");
    if (Boolean.parseBoolean(propertyValue)) {
        Date priorTo = DateUtils.truncate(DateUtils.addDays(new Date(), -daysPrior), Calendar.DATE);
        int rowCount = this.simpleJdbcTemplate.update("delete from schedules where end_time < ?", priorTo);
        LOG.warn("purged " + rowCount + " rows from schedules table with end_time values prior to: " + priorTo);
        return rowCount;
    } else {
        LOG.debug("ignoring purgeExpiredBlocks as 'org.jasig.schedassist.runScheduledTasks' set to false");
        return 0;
    }
}

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

/**
 * //from w  w w.  ja va  2s.c  o m
 * @param startTime
 * @param endTime
 * @param visitorLimit
 * @param meetingLocation
 * @throws IllegalArgumentException if startTime/endTime are null, or if endTime is before or equal to startTime, or if visitorLimit is less than 1 
 */
AvailableBlock(final Date startTime, final Date endTime, final int visitorLimit, String meetingLocation) {
    Validate.notNull(startTime, "startTime cannot be null");
    Validate.notNull(endTime, "endTime cannot be null");
    if (endTime.before(startTime) || endTime.equals(startTime)) {
        throw new IllegalArgumentException(
                "startTime (" + startTime + ") must precede endTime (" + endTime + ")");
    }
    if (visitorLimit < 1) {
        throw new IllegalArgumentException("visitorLimit must be greater than or equal to 1: " + visitorLimit);
    }
    this.startTime = DateUtils.truncate(startTime, Calendar.MINUTE);
    this.endTime = DateUtils.truncate(endTime, Calendar.MINUTE);
    this.visitorLimit = visitorLimit;
    this.meetingLocation = meetingLocation;
}

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

/**
 * /* w w w  .j av a 2s .c om*/
 * @param startTimePhrase
 * @param endTimePhrase
 * @param daysOfWeekPhrase
 * @param startDate
 * @param endDate
 * @param visitorLimit
 * @param meetingLocation
 * @return
 * @throws InputFormatException
 */
public static SortedSet<AvailableBlock> createBlocks(final String startTimePhrase, final String endTimePhrase,
        final String daysOfWeekPhrase, final Date startDate, final Date endDate, final int visitorLimit,
        final String meetingLocation) throws InputFormatException {
    SortedSet<AvailableBlock> blocks = new TreeSet<AvailableBlock>();
    // set time of startDate to 00:00:00
    Date realStartDate = DateUtils.truncate(startDate, Calendar.DATE);
    // set time of endDate to 23:59:59
    Date dayAfterEndDate = DateUtils.truncate(DateUtils.addDays(endDate, 1), Calendar.DATE);
    Date realEndDate = DateUtils.addSeconds(dayAfterEndDate, -1);

    if (LOG.isDebugEnabled()) {
        LOG.debug("createBlocks calculated realStartDate: " + realStartDate + ", realEndDate: " + realEndDate);
    }

    List<Date> matchingDays = matchingDays(daysOfWeekPhrase, realStartDate, realEndDate);
    for (Date matchingDate : matchingDays) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(matchingDate);
        Calendar endCalendar = (Calendar) startCalendar.clone();

        interpretAndUpdateTime(startTimePhrase, startCalendar);
        interpretAndUpdateTime(endTimePhrase, endCalendar);

        Date blockStartTime = startCalendar.getTime();
        Date blockEndTime = endCalendar.getTime();
        if (!blockEndTime.after(blockStartTime)) {
            throw new InputFormatException("Start time must occur before end time");
        }
        if (CommonDateOperations.equalsOrAfter(blockStartTime, realStartDate)
                && CommonDateOperations.equalsOrBefore(blockEndTime, realEndDate)) {
            AvailableBlock block = new AvailableBlock(blockStartTime, blockEndTime, visitorLimit,
                    meetingLocation);
            blocks.add(block);
        }

    }
    return blocks;
}

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

/**
 * Returns the Sunday prior to the date argument
 * elements of the returned array, respectively.
 * @param date//from   w  w w.j  a va  2s  .  c o  m
 * @return the sunday prior to the date argument
 */
public static Date calculateSundayPrior(final Date date) {
    Calendar weekOfCal = Calendar.getInstance();
    weekOfCal.setTime(date);
    weekOfCal = DateUtils.truncate(weekOfCal, Calendar.DATE);

    int dayOfWeek = weekOfCal.get(Calendar.DAY_OF_WEEK);

    // dayOfWeek ranges 1-7
    int differenceToSunday = (dayOfWeek % 7) - 1;
    // subtract difference to sunday
    weekOfCal.add(Calendar.DATE, -differenceToSunday);
    return weekOfCal.getTime();
}

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

/**
 * Convert a {@link String} in the common date/time format for this application into a {@link Date}.
 * /*from   w  w w.ja v a2s  . c o  m*/
 * @param timePhrase format: "yyyyMMdd-HHmm"
 * @return the corresponding date
 * @throws InputFormatException
 */
public static Date parseDateTimePhrase(final String timePhrase) throws InputFormatException {
    try {
        Date time = getDateTimeFormat().parse(timePhrase);
        time = DateUtils.truncate(time, Calendar.MINUTE);
        return time;
    } catch (ParseException e) {
        throw new InputFormatException("cannot parse date/time phrase " + timePhrase, e);
    }

}

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

/**
 * Convert a {@link String} in the common date format for this application into a {@link Date}.
 * /*from   w  w w . ja  v a 2  s  .co m*/
 * @param datePhrase format: "yyyyMMdd"
 * @return the corresponding date
 * @throws InputFormatException
 */
public static Date parseDatePhrase(final String datePhrase) throws InputFormatException {
    try {
        Date date = getDateFormat().parse(datePhrase);
        date = DateUtils.truncate(date, Calendar.DATE);
        return date;
    } catch (ParseException e) {
        throw new InputFormatException("cannot parse date phrase " + datePhrase, e);
    }
}