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.sonar.plugins.core.issue.InitialOpenIssuesSensor.java

@Override
public void analyse(Project project, SensorContext context) {
    // Adding one second is a hack for resolving conflicts with concurrent user
    // changes during issue persistence
    final Date now = DateUtils.addSeconds(DateUtils.truncate(new Date(), Calendar.MILLISECOND), 1);

    issueDao.selectNonClosedIssuesByModule(project.getId(), new ResultHandler() {
        @Override/*  ww w . j  a va2s . c  o  m*/
        public void handleResult(ResultContext rc) {
            IssueDto dto = (IssueDto) rc.getResultObject();
            dto.setSelectedAt(now);
            initialOpenIssuesStack.addIssue(dto);
        }
    });
}

From source file:org.sonar.server.filters.DateCriterion.java

public DateCriterion setDate(int daysAgo) {
    this.date = DateUtils.addDays(new Date(), -daysAgo);
    this.date = DateUtils.truncate(this.date, Calendar.DATE);
    return this;
}

From source file:org.sonar.server.issue.IssueFieldsSetterTest.java

@Test
public void setIssueMoved_changes_componentUuid_adds_a_change() {
    String oldComponentUuid = "a";
    String newComponentUuid = "b";
    issue.setComponentUuid(oldComponentUuid);

    updater.setIssueMoved(issue, newComponentUuid, context);

    assertThat(issue.changes()).hasSize(1);
    FieldDiffs fieldDiffs = issue.changes().get(0);
    assertThat(fieldDiffs.creationDate()).isEqualTo(context.date());
    assertThat(fieldDiffs.diffs()).hasSize(1);
    Map.Entry<String, FieldDiffs.Diff> entry = fieldDiffs.diffs().entrySet().iterator().next();
    assertThat(entry.getKey()).isEqualTo("file");
    assertThat(entry.getValue().oldValue()).isEqualTo(oldComponentUuid);
    assertThat(entry.getValue().newValue()).isEqualTo(newComponentUuid);
    assertThat(issue.componentUuid()).isEqualTo(newComponentUuid);
    assertThat(issue.isChanged()).isTrue();
    assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(context.date(), Calendar.SECOND));
}

From source file:org.sonar.server.issue.IssueUpdater.java

public void setCloseDate(DefaultIssue issue, @Nullable Date d, IssueChangeContext context) {
    Date dateWithoutMilliseconds = d == null ? null : DateUtils.truncate(d, Calendar.SECOND);
    if (!Objects.equals(dateWithoutMilliseconds, issue.closeDate())) {
        issue.setCloseDate(d);/*from www. jav a 2s .com*/
        issue.setUpdateDate(context.date());
        issue.setChanged(true);
    }
}

From source file:org.sonar.server.issue.workflow.IssueWorkflowTest.java

@Test
public void automatically_close_resolved_issue() {
    workflow.start();//  w  w  w  .j  a  va 2s  .  co m

    DefaultIssue issue = new DefaultIssue().setKey("ABCDE").setRuleKey(RuleKey.of("js", "S001"))
            .setResolution(RESOLUTION_FIXED).setStatus(STATUS_RESOLVED).setNew(false).setBeingClosed(true);
    Date now = new Date();
    workflow.doAutomaticTransition(issue, IssueChangeContext.createScan(now));
    assertThat(issue.resolution()).isEqualTo(RESOLUTION_FIXED);
    assertThat(issue.status()).isEqualTo(STATUS_CLOSED);
    assertThat(issue.closeDate()).isNotNull();
    assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND));
}

From source file:org.sonar.server.issue.workflow.IssueWorkflowTest.java

@Test
public void close_open_dead_issue() {
    workflow.start();//from w w  w.j a v  a 2s .  c  o  m

    DefaultIssue issue = new DefaultIssue().setKey("ABCDE").setResolution(null).setStatus(STATUS_OPEN)
            .setNew(false).setBeingClosed(true);
    Date now = new Date();
    workflow.doAutomaticTransition(issue, IssueChangeContext.createScan(now));
    assertThat(issue.resolution()).isEqualTo(RESOLUTION_FIXED);
    assertThat(issue.status()).isEqualTo(STATUS_CLOSED);
    assertThat(issue.closeDate()).isNotNull();
    assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND));
}

From source file:org.sonar.server.issue.workflow.IssueWorkflowTest.java

@Test
public void close_reopened_dead_issue() {
    workflow.start();//from   w w  w  .ja va2 s  . co  m

    DefaultIssue issue = new DefaultIssue().setKey("ABCDE").setResolution(null).setStatus(STATUS_REOPENED)
            .setNew(false).setBeingClosed(true);
    Date now = new Date();
    workflow.doAutomaticTransition(issue, IssueChangeContext.createScan(now));
    assertThat(issue.resolution()).isEqualTo(RESOLUTION_FIXED);
    assertThat(issue.status()).isEqualTo(STATUS_CLOSED);
    assertThat(issue.closeDate()).isNotNull();
    assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND));
}

From source file:org.sonar.server.issue.workflow.IssueWorkflowTest.java

@Test
public void close_confirmed_dead_issue() {
    workflow.start();/*w  w w . j  av  a2s.  co  m*/

    DefaultIssue issue = new DefaultIssue().setKey("ABCDE").setResolution(null).setStatus(STATUS_CONFIRMED)
            .setNew(false).setBeingClosed(true);
    Date now = new Date();
    workflow.doAutomaticTransition(issue, IssueChangeContext.createScan(now));
    assertThat(issue.resolution()).isEqualTo(RESOLUTION_FIXED);
    assertThat(issue.status()).isEqualTo(STATUS_CLOSED);
    assertThat(issue.closeDate()).isNotNull();
    assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(now, Calendar.SECOND));
}

From source file:org.vpac.ndg.storage.util.TimeSliceUtil.java

/**
 * Find appropriate units for the time dimension.
 * /* w  w  w  .  j  a  v a2 s.c om*/
 * @param timeSlices
 *            The time slices to find a unit for.
 * @param dates
 *            A list of coordinates in the new units, which map 1:1 to the
 *            given time slices. This should be an empty list; it will be
 *            populated by this method.
 * @return A description the new units.
 */
public CalendarDateUnit computeTimeMapping(List<TimeSlice> timeSlices, List<CalendarDate> dates) {

    // We need to know the smallest precision of the selected time slices,
    // so that a sensible value can be used in the exported NCML: the
    // coordinates will use units like "Days since 2011-01-01". See:
    // http://www.unidata.ucar.edu/software/netcdf/docs/BestPractices.html#Calendar%20Date/Time
    // http://www.unidata.ucar.edu/software/netcdf/time/recs.html

    Date mindate = null;
    long minprecision = Long.MAX_VALUE;
    for (TimeSlice ts : timeSlices) {
        if (mindate == null || ts.getCreated().before(mindate))
            mindate = ts.getCreated();
        Dataset ds = timeSliceDao.getParentDataset(ts.getId());
        if (minprecision == Long.MAX_VALUE || ds.getPrecision() < minprecision)
            minprecision = ds.getPrecision();
    }

    // Find an epoch: truncate the date to the nearest sensible major value,
    // e.g. to the nearest year.
    Date epoch;
    String timeUnits;

    if (minprecision < DateUtils.MILLIS_PER_SECOND) {
        // Precision of less than one second; round to milliseconds.
        epoch = DateUtils.truncate(mindate, Calendar.SECOND);
        timeUnits = String.format("msec since %s", DateFormatUtils.format(epoch, Default.SECOND_PATTERN));

    } else if (minprecision < DateUtils.MILLIS_PER_MINUTE) {
        // Precision of less than one minute; round to seconds.
        epoch = DateUtils.truncate(mindate, Calendar.MINUTE);
        timeUnits = String.format("Seconds since %s", DateFormatUtils.format(epoch, Default.MINUTE_PATTERN));

    } else if (minprecision < DateUtils.MILLIS_PER_HOUR) {
        // Precision of less than one hour; round to minutes.
        epoch = DateUtils.truncate(mindate, Calendar.HOUR);
        timeUnits = String.format("Minutes since %s", DateFormatUtils.format(epoch, Default.HOUR_PATTERN));

    } else if (minprecision < DateUtils.MILLIS_PER_DAY) {
        // Precision of less than one day; round to hours.
        epoch = DateUtils.truncate(mindate, Calendar.DAY_OF_MONTH);
        timeUnits = String.format("Hours since %s", DateFormatUtils.format(epoch, Default.DAY_PATTERN));

    } else {
        // Precision GREATER than one day; round to days.
        epoch = DateUtils.truncate(mindate, Calendar.YEAR);
        timeUnits = String.format("Days since %s", DateFormatUtils.format(epoch, Default.DAY_PATTERN));
    }

    CalendarDateUnit units = CalendarDateUnit.of("proleptic_gregorian", timeUnits);

    // Calculate a set of new coordinates for each time slice, relative to
    // the epoch.
    for (TimeSlice ts : timeSlices) {
        CalendarDate coordinate = CalendarDate.of(ts.getCreated().getTime());
        dates.add(coordinate);
    }

    return units;
}

From source file:ru.codeinside.calendar.BusinessCalendarDueDateCalculator.java

private boolean isWorkedDay(Calendar calendar) {
    Date date = DateUtils.truncate(calendar.getTime(), Calendar.DAY_OF_MONTH);
    boolean isHoliday = isWeekEnd(calendar) || holidays.contains(date);
    boolean isWorkDay = workedDays.contains(date);
    return !isHoliday || isWorkDay;
}