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

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

Introduction

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

Prototype

public static Date truncate(final Object date, final int field) 

Source Link

Document

Truncates a date, leaving the field specified as the most significant field.

For example, if you had the date-time 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:com.norconex.commons.lang.time.YearMonthDay.java

/**
 * Converts this YearMonthDay to a {@link Calendar} at midnight.
 * @return a calendar//from   ww  w . j  a v  a 2  s .c  o m
 */
public Calendar toCalendar() {
    Calendar cal = Calendar.getInstance();
    cal.set(year, month - 1, day);
    cal = DateUtils.truncate(cal, Calendar.DAY_OF_MONTH);
    return cal;
}

From source file:com.webbfontaine.valuewebb.action.pricedb.mp.MPCriteriaContainer.java

public static Criterion transformAgeOfPriceToPriceDate(UserCriterion ageOfPriceCriterion,
        UserCriterion criterionToModify) throws Exception {

    if (ageOfPriceCriterion.getValue() == null) {
        return null;
    }//from  w w w. j  a  va2  s . com

    Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
    long userInputedDaysAmount = (Long) ageOfPriceCriterion.getValue();
    Date todayMinusUserInputedDaysAmount = DateUtils.addDays(today, -(int) userInputedDaysAmount);
    criterionToModify.setValue(todayMinusUserInputedDaysAmount);

    String ageOfPriceCriterionCondition = ageOfPriceCriterion.getCondition();
    switch (ageOfPriceCriterionCondition) {
    case StringConditions.GE:
        criterionToModify.setCondition(StringConditions.LE);
        break;
    case StringConditions.LE:
        criterionToModify.setCondition(StringConditions.GE);
        break;
    case StringConditions.GT:
        criterionToModify.setCondition(StringConditions.LT);
        break;
    case StringConditions.LT:
        criterionToModify.setCondition(StringConditions.GT);
        break;
    default:
        criterionToModify.setCondition(ageOfPriceCriterionCondition);
        break;
    }

    return criterionToModify.transform();
}

From source file:io.lavagna.web.api.CardController.java

@ExpectPermission(Permission.READ)
@RequestMapping(value = "/api/project/{projectShortName}/cards-by-milestone-detail/{milestone}", method = RequestMethod.GET)
public MilestoneDetail findCardsByMilestoneDetail(@PathVariable("projectShortName") String projectShortName,
        @PathVariable("milestone") String milestone, UserWithPermission user) {

    int projectId = projectService.findByShortName(projectShortName).getId();
    CardLabel label = cardLabelRepository.findLabelByName(projectId, "MILESTONE", CardLabel.LabelDomain.SYSTEM);
    List<LabelListValueWithMetadata> listValues = cardLabelRepository
            .findListValuesByLabelIdAndValue(label.getId(), milestone);

    SearchFilter filter;/*from  ww w . j  a va  2s. co m*/
    Map<Long, Pair<Long, Long>> assignedAndClosedCards;

    if (listValues.size() > 0) {
        filter = filter(FilterType.MILESTONE, ValueType.STRING, milestone);
        assignedAndClosedCards = statisticsService.getAssignedAndClosedCardsByMilestone(listValues.get(0),
                DateUtils.addWeeks(DateUtils.truncate(new Date(), Calendar.DATE), -2));
    } else {
        filter = filter(FilterType.MILESTONE, ValueType.UNASSIGNED, null);
        assignedAndClosedCards = null;
    }

    SearchFilter notTrashFilter = filter(SearchFilter.FilterType.NOTLOCATION, SearchFilter.ValueType.STRING,
            BoardColumnLocation.TRASH.toString());

    SearchResults cards = searchService.find(Arrays.asList(filter, notTrashFilter), projectId, null, user);
    return new MilestoneDetail(cards, assignedAndClosedCards);
}

From source file:de.tor.tribes.types.TimeSpan.java

public boolean intersects(TimeSpan pSpan) {
    if (!this.getDirection().equals(pSpan.getDirection())) {
        //different directions
        return false;
    }// w w w  .  j a v  a  2  s. c  om

    //one of the spans uses manual Time (new intersect)
    Range<Long> thisSpan = this.getSpan();
    Range<Long> theOtherSpan = pSpan.getSpan();

    if (this.isValidAtEveryDay() || pSpan.isValidAtEveryDay()) {
        if (this.isValidAtSpecificDay() || pSpan.isValidAtSpecificDay()) {
            //remove day Information
            Long thisStart = DateUtils.getFragmentInMilliseconds(new Date(thisSpan.getMinimum()),
                    Calendar.DATE);
            Long thisEnd = DateUtils.getFragmentInMilliseconds(new Date(thisSpan.getMaximum()), Calendar.DATE);
            thisSpan = Range.between(thisStart, thisEnd);

            Long otherStart = DateUtils.getFragmentInMilliseconds(new Date(theOtherSpan.getMinimum()),
                    Calendar.DATE);
            Long otherEnd = DateUtils.getFragmentInMilliseconds(new Date(theOtherSpan.getMaximum()),
                    Calendar.DATE);

            theOtherSpan = Range.between(otherStart, otherEnd);

            return thisSpan.isOverlappedBy(theOtherSpan);
        } else if (this.isValidAtEveryDay() && pSpan.isValidAtEveryDay()) {
            //both valid at every Day - just compare spans
            return thisSpan.isOverlappedBy(theOtherSpan);
        } else {
            //one span is for everyDay the other is over multiple Days
            //manual intersect
            Range<Long> always;
            Range<Long> manual;
            if (this.isValidAtEveryDay()) {
                always = thisSpan;
                manual = theOtherSpan;
            } else {
                always = theOtherSpan;
                manual = thisSpan;
            }

            long manualDate = DateUtils.truncate(new Date(manual.getMinimum()), Calendar.DATE).getTime();
            long manualStart = manual.getMinimum() - manualDate;
            long manualEnd = manual.getMaximum() - manualDate;

            if (manualEnd - manualStart > DateUtils.MILLIS_PER_DAY) {
                //must intersect somehow because span is longer than 1 Day
                return true;
            }
            //direct intersection
            manual = Range.between(manualStart, manualEnd);
            if (always.isOverlappedBy(manual))
                return true;

            //should not be possible, because it should be handeld by isValidAtSpecificDay
            if (manualEnd <= DateUtils.MILLIS_PER_DAY)
                return false;

            //maybe intersection at next day
            manual = Range.between(new Long(0), manualEnd - DateUtils.MILLIS_PER_DAY);
            return always.isOverlappedBy(manual);
        }
    }

    return thisSpan.isOverlappedBy(theOtherSpan);
}

From source file:gov.nih.nci.firebird.common.FirebirdDateUtilsTest.java

@Test
public void testParseMonthAndYearStringOrNull() throws ParseException {
    String dateString = "09/2012";
    Date date = parseMonthAndYearStringOrNull(dateString);
    assertEquals(DateUtils.truncate(testDate, Calendar.MONTH), date);
}

From source file:models.MilestoneTest.java

@Test
public void untilOver() {
    // Given//w w w  . j av  a 2  s  . co  m
    int days = 3;
    Milestone milestone = new Milestone();
    milestone.dueDate = DateUtils.truncate(DateUtils.addDays(new Date(), -days), Calendar.DATE);

    // When
    String until = milestone.until();

    // Then
    assertThat(until).isEqualTo(Messages.get("common.time.overday", days));
}

From source file:de.tor.tribes.util.algo.types.TimeFrame.java

public List<Range<Long>> startTimespansToRanges() {
    List<Range<Long>> ranges = new LinkedList<>();
    Date startDate = DateUtils.truncate(new Date(startNotBefore), Calendar.DATE);

    for (TimeSpan span : sendTimeSpans) {
        if (!span.isValidAtEveryDay()) {
            Range<Long> range;
            //just copy range
            if (span.isValidAtExactTime()) {
                range = Range.between(span.getSpan().getMinimum(),
                        span.getSpan().getMaximum() + fixedStartTimeRangeSize);
            } else {
                range = Range.between(span.getSpan().getMinimum(), span.getSpan().getMaximum());
            }/*from w  w w  . j  a  va2 s  .  c  o m*/

            if (range.getMaximum() > System.currentTimeMillis()) {
                if (range.getMinimum() <= System.currentTimeMillis()) {
                    //rebuild Range
                    range = Range.between(System.currentTimeMillis(), range.getMaximum());
                }
                //add range only if it is in future
                ranges.add(range);
            }
        } else {
            //span is valid for every day
            Date thisDate = new Date(startDate.getTime());
            //go through all days from start to end
            while (thisDate.getTime() < startNotAfter) {
                long spanStart = thisDate.getTime() + span.getSpan().getMinimum();
                long spanEnd = thisDate.getTime() + span.getSpan().getMaximum();
                Range<Long> newRange = null;
                //check span location relative to start frame
                if (spanStart >= startNotBefore && spanEnd > startNotBefore && spanStart < startNotAfter
                        && spanEnd <= startNotAfter) {
                    //|----------| (startNotBefore - startNotAfter)
                    //  |----| (SpanStart - SpanEnd)
                    newRange = Range.between(spanStart, spanEnd);
                } else if (spanStart < startNotBefore && spanEnd > startNotBefore && spanStart < startNotAfter
                        && spanEnd <= startNotAfter) {
                    //  |----------| (startNotBefore - startNotAfter)
                    //|----| (SpanStart - SpanEnd)
                    //set span start to 'startNotBefore'
                    newRange = Range.between(startNotBefore, spanEnd);
                } else if (spanStart <= startNotBefore && spanEnd > startNotBefore && spanStart > startNotAfter
                        && spanEnd >= startNotAfter) {
                    //  |----------| (startNotBefore - startNotAfter)
                    //|--------------| (SpanStart - SpanEnd)
                    //set span start to 'startNotBefore'
                    newRange = Range.between(startNotBefore, startNotAfter);
                } else if (spanStart >= startNotBefore && spanEnd > startNotBefore && spanStart < startNotAfter
                        && spanEnd >= startNotAfter) {
                    //|----------| (startNotBefore - startNotAfter)
                    //    |---------| (SpanStart - SpanEnd)
                    //set span start to 'startNotBefore'
                    newRange = Range.between(spanStart, startNotAfter);
                }

                if (newRange != null) {
                    if (newRange.getMinimum() < System.currentTimeMillis()) {
                        //check minimum as current minimum is in past
                        if (newRange.getMaximum() > System.currentTimeMillis()) {
                            newRange = Range.between(System.currentTimeMillis(), newRange.getMaximum());
                            ranges.add(newRange);
                        } //ignore as entire range is in past
                    } else {
                        //add range as it is in future
                        ranges.add(newRange);
                    }
                }
                //increment current date by one day
                thisDate = DateUtils.addDays(thisDate, 1);
            }
        }
    }
    Collections.sort(ranges, new Comparator<Range<Long>>() {
        @Override
        public int compare(Range<Long> o1, Range<Long> o2) {
            return o1.getMinimum().compareTo(o2.getMinimum());
        }
    });
    return ranges;
}

From source file:models.MilestoneTest.java

@Test
public void untilToday() {
    // Given//from www .  j  a v a  2s  . c  o  m
    Milestone milestone = new Milestone();
    milestone.dueDate = DateUtils.truncate(new Date(), Calendar.DATE);

    // When
    String until = milestone.until();

    // Then
    assertThat(until).isEqualTo(Messages.get("common.time.today"));
}

From source file:fi.foyt.fni.view.users.UsersProfileBackingBean.java

private Event createEventPojo(IllusionEvent event) {
    if (DateUtils.isSameDay(event.getStart(), event.getEnd())) {
        Date date = DateUtils.truncate(event.getStart(), Calendar.DAY_OF_MONTH);
        long startDiff = event.getStart().getTime() - date.getTime();
        long endDiff = event.getEnd().getTime() - date.getTime();
        Date startTime = startDiff > 0 ? new Date(startDiff) : null;
        Date endTime = endDiff > 0 ? new Date(endDiff) : null;
        return new Event(event.getName(), event.getUrlName(), event.getDescription(), event.getStart(),
                startTime, event.getEnd(), endTime);
    } else {//ww w . j  a  v  a 2s. c om
        return new Event(event.getName(), event.getUrlName(), event.getDescription(), event.getStart(), null,
                event.getEnd(), null);
    }
}

From source file:models.MilestoneTest.java

@Test
public void untilLeft() {
    // Given/*from w  ww.ja v  a 2  s  .c om*/
    int days = 3;
    Milestone milestone = new Milestone();
    milestone.dueDate = DateUtils.truncate(DateUtils.addDays(new Date(), days), Calendar.DATE);

    // When
    String until = milestone.until();

    // Then
    assertThat(until).isEqualTo(Messages.get("common.time.leftday", days));
}