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

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

Introduction

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

Prototype

public static Date addHours(final Date date, final int amount) 

Source Link

Document

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

Usage

From source file:com.thoughtworks.go.domain.JobInstanceTest.java

@Test
public void shouldDetermineMostRecentPassed() {
    JobInstance oldestPassed = JobInstanceMother.building("oldestPassed");
    oldestPassed.completing(JobResult.Passed, DateUtils.addHours(new Date(), -1));
    oldestPassed.completed(DateUtils.addHours(new Date(), -1));

    JobInstance newestPassed = JobInstanceMother.building("newestPassed");
    newestPassed.completing(JobResult.Passed, new Date());
    newestPassed.completed(new Date());

    assertEquals(newestPassed, newestPassed.mostRecentPassed(oldestPassed));
    assertEquals(newestPassed, oldestPassed.mostRecentPassed(newestPassed));

    JobInstance newestFailed = JobInstanceMother.building("newestFailed");
    newestFailed.completing(JobResult.Failed, DateUtils.addHours(new Date(), +1));
    newestFailed.completed(DateUtils.addHours(new Date(), +1));

    assertEquals(newestPassed, newestPassed.mostRecentPassed(newestFailed));
}

From source file:com.monco.calendarofveeshan.RaidTarget.java

public void setTimes(Date killTime, int respawnHours, int respawnWindow) {
    this.lastKillTime = killTime;
    nxAvgSpawnTime = DateUtils.addHours(killTime, respawnHours);
    windowClose = DateUtils.addHours(killTime, (respawnWindow / 2));
    windowOpen = DateUtils.addHours(killTime, (-1) * (respawnWindow / 2));
}

From source file:eionet.webq.task.RemoveExpiredUserFilesTask.java

/**
 * Perform removal task./*from   w  ww .j  a v a2s  .  co m*/
 */
@Scheduled(cron = "0 0 0 * * *")
@Transactional
public void removeExpiredUserFiles() {
    Integer hoursAgo = getExpirationHours();
    Date allowedDate = DateUtils.addHours(new Date(), -hoursAgo);
    LOGGER.info("Removing user files last modified before " + allowedDate + "(in storage more than " + hoursAgo
            + " hours). ");

    Session currentSession = factory.getCurrentSession();
    List rowsAffected = currentSession.createCriteria(UserFile.class)
            .add(Restrictions.le("updated", new Timestamp(allowedDate.getTime()))).list();
    for (Object row : rowsAffected) {
        currentSession.delete(row);
    }

    LOGGER.info("Removal successful. Removed " + rowsAffected + " files.");
}

From source file:info.magnolia.ui.framework.command.CleanTempFilesCommand.java

@Override
public boolean execute(final Context context) throws Exception {
    String tmpDirPath = configurationProperties.getProperty("magnolia.upload.tmpdir");
    File tmpDir = new File(tmpDirPath);
    Date date = DateUtils.addHours(new Date(), TIME_OFFSET_IN_HOURS); // current time minus 12 hours
    Iterator<File> files = FileUtils.iterateFiles(tmpDir, FileFilterUtils.ageFileFilter(date),
            FileFilterUtils.ageFileFilter(date));
    while (files.hasNext()) {
        files.next().delete();//from   w w w .  j  a  v a 2 s .c  o m
    }
    return true;
}

From source file:com.stratelia.webactiv.util.DateUtilTest.java

@Test
public void testGetOutputDateAndHour() {
    Date date = DateUtil.resetHour(java.sql.Date.valueOf("2013-05-21"));
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("2013/05/21"));

    Date year = DateUtils.addYears(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(year, LANGUAGE), is("2014/05/21"));

    Date month = DateUtils.addMonths(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(month, LANGUAGE), is("2013/06/21"));

    Date day = DateUtils.addDays(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(day, LANGUAGE), is("2013/05/22"));

    Date hour = DateUtils.addHours(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(hour, LANGUAGE), is("2013/05/21 01:00"));
    hour = DateUtils.addHours(date, 12);
    assertThat(DateUtil.getOutputDateAndHour(hour, LANGUAGE), is("2013/05/21 12:00"));
    hour = DateUtils.addHours(date, 22);
    assertThat(DateUtil.getOutputDateAndHour(hour, LANGUAGE), is("2013/05/21 22:00"));

    Date minute = DateUtils.addMinutes(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("2013/05/21 00:01"));
    minute = DateUtils.addMinutes(date, 59);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("2013/05/21 00:59"));
    minute = DateUtils.addMinutes(date, 60);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("2013/05/21 01:00"));
    minute = DateUtils.addMinutes(date, 61);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("2013/05/21 01:01"));

    Date second = DateUtils.addSeconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("2013/05/21 00:00"));
    second = DateUtils.addSeconds(date, 59);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("2013/05/21 00:00"));
    second = DateUtils.addSeconds(date, 60);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("2013/05/21 00:01"));
    second = DateUtils.addSeconds(date, 61);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("2013/05/21 00:01"));

    Date millisecond = DateUtils.addMilliseconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("2013/05/21 00:00"));
    millisecond = DateUtils.addMilliseconds(date, 999);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("2013/05/21 00:00"));
    millisecond = DateUtils.addMilliseconds(date, 1000);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("2013/05/21 00:00"));
    millisecond = DateUtils.addMilliseconds(date, 1001);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("2013/05/21 00:00"));

    // 2013-05-21 23:59:59.999
    date = DateUtils.addHours(// w w  w  .  ja v a 2  s  .c o  m
            DateUtils.addMinutes(DateUtils.addSeconds(DateUtils.addMilliseconds(date, 999), 59), 59), 23);
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("2013/05/21 23:59"));

    // 2013-05-22 00:00:00.000
    date = DateUtils.addMilliseconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("2013/05/22"));

    // 2013-05-22 00:00:00.001
    date = DateUtils.addMilliseconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("2013/05/22 00:00"));
}

From source file:com.green.modules.cms.service.ArticleService.java

@Transactional(readOnly = false)
public Page<Article> find(Page<Article> page, Article article, boolean isDataScopeFilter) {
    // ??6??/* w w w.j  av a2  s.c o  m*/
    Date updateExpiredWeightDate = (Date) CacheUtils.get("updateExpiredWeightDateByArticle");
    if (updateExpiredWeightDate == null
            || (updateExpiredWeightDate != null && updateExpiredWeightDate.getTime() < new Date().getTime())) {
        articleDao.updateExpiredWeight();
        CacheUtils.put("updateExpiredWeightDateByArticle", DateUtils.addHours(new Date(), 6));
    }
    DetachedCriteria dc = articleDao.createDetachedCriteria();
    dc.createAlias("category", "category");
    dc.createAlias("category.site", "category.site");
    if (article.getCategory() != null && StringUtils.isNotBlank(article.getCategory().getId())
            && !Category.isRoot(article.getCategory().getId())) {
        Category category = categoryDao.get(article.getCategory().getId());
        if (category != null) {
            dc.add(Restrictions.or(Restrictions.eq("category.id", category.getId()),
                    Restrictions.like("category.parentIds", "%," + category.getId() + ",%")));
            dc.add(Restrictions.eq("category.site.id", category.getSite().getId()));
            article.setCategory(category);
        } else {
            dc.add(Restrictions.eq("category.site.id", Site.getCurrentSiteId()));
        }
    } else {
        dc.add(Restrictions.eq("category.site.id", Site.getCurrentSiteId()));
    }
    if (StringUtils.isNotEmpty(article.getTitle())) {
        dc.add(Restrictions.like("title", "%" + article.getTitle() + "%"));
    }
    if (StringUtils.isNotEmpty(article.getPosid())) {
        dc.add(Restrictions.like("posid", "%," + article.getPosid() + ",%"));
    }
    if (StringUtils.isNotEmpty(article.getImage()) && Article.YES.equals(article.getImage())) {
        dc.add(Restrictions.and(Restrictions.isNotNull("image"), Restrictions.ne("image", "")));
    }
    if (article.getCreateBy() != null && StringUtils.isNotBlank(article.getCreateBy().getId())) {
        dc.add(Restrictions.eq("createBy.id", article.getCreateBy().getId()));
    }
    if (isDataScopeFilter) {
        dc.createAlias("category.office", "categoryOffice").createAlias("createBy", "createBy");
        dc.add(dataScopeFilter(UserUtils.getUser(), "categoryOffice", "createBy"));
    }
    dc.add(Restrictions.eq(Article.FIELD_DEL_FLAG, article.getDelFlag()));
    if (StringUtils.isBlank(page.getOrderBy())) {
        dc.addOrder(Order.desc("weight"));
        dc.addOrder(Order.desc("updateDate"));
    }
    return articleDao.find(page, dc);
}

From source file:com.hongqiang.shop.modules.cms.service.ArticleService.java

public Page<Article> find(Page<Article> page, Article article, boolean isDataScopeFilter) {
    // ??6??/*w  w  w .  j  av  a  2  s .c  o m*/
    Date updateExpiredWeightDate = (Date) CacheUtils.get("updateExpiredWeightDateByArticle");
    if (updateExpiredWeightDate == null
            || (updateExpiredWeightDate != null && updateExpiredWeightDate.getTime() < new Date().getTime())) {
        articleDao.updateExpiredWeight();
        CacheUtils.put("updateExpiredWeightDateByArticle", DateUtils.addHours(new Date(), 6));
    }
    DetachedCriteria dc = articleDao.createDetachedCriteria();
    dc.createAlias("category", "category");
    dc.createAlias("category.site", "category.site");
    if (article.getCategory() != null && article.getCategory().getId() != null
            && !Category.isRoot(article.getCategory().getId())) {
        Category category = categoryDao.findOne(article.getCategory().getId());
        if (category != null) {
            dc.add(Restrictions.or(Restrictions.eq("category.id", category.getId()),
                    Restrictions.like("category.parentIds", "%," + category.getId() + ",%")));
            dc.add(Restrictions.eq("category.site.id", category.getSite().getId()));
            article.setCategory(category);
        } else {
            dc.add(Restrictions.eq("category.site.id", Site.getCurrentSiteId()));
        }
    } else {
        dc.add(Restrictions.eq("category.site.id", Site.getCurrentSiteId()));
    }
    if (StringUtils.isNotEmpty(article.getTitle())) {
        dc.add(Restrictions.like("title", "%" + article.getTitle() + "%"));
    }
    if (StringUtils.isNotEmpty(article.getPosid())) {
        dc.add(Restrictions.like("posid", "%," + article.getPosid() + ",%"));
    }
    if (StringUtils.isNotEmpty(article.getImage()) && Article.YES.equals(article.getImage())) {
        dc.add(Restrictions.and(Restrictions.isNotNull("image"), Restrictions.ne("image", "")));
    }
    if (article.getCreateBy() != null && article.getCreateBy().getId() > 0) {
        dc.add(Restrictions.eq("createBy.id", article.getCreateBy().getId()));
    }
    if (isDataScopeFilter) {
        dc.createAlias("category.office", "categoryOffice").createAlias("createBy", "createBy");
        dc.add(dataScopeFilter(UserUtils.getUser(), "categoryOffice", "createBy"));
    }
    dc.add(Restrictions.eq(Article.DEL_FLAG, article.getDelFlag()));
    if (StringUtils.isBlank(page.getOrderBy())) {
        dc.addOrder(Order.desc("weight"));
        dc.addOrder(Order.desc("updateDate"));
    }
    return articleDao.find(page, dc);
}

From source file:eionet.webq.task.RemoveExpiredUserFilesTaskTest.java

@Test
public void performsRemovalBasedOnConfiguredProperties() throws Exception {
    removeExpiredUserFilesTask.setExpirationHours("1");
    removeExpiredUserFilesTask.removeExpiredUserFiles();

    ArgumentCaptor<SimpleExpression> criterionCaptor = ArgumentCaptor.forClass(SimpleExpression.class);
    verify(criteria).add(criterionCaptor.capture());

    Date expectedDate = DateUtils.addHours(new Date(), -expirationTimeInHours);
    Timestamp date = (Timestamp) criterionCaptor.getValue().getValue();
    assertEquals(expectedDate.getTime(), date.getTime(), 1000);
}

From source file:net.audumla.astronomy.algorithims.EllipticalObject.java

@Override
public TransitDetails getTransitDetails(Date date, Geolocation location, double altitude) {
    // to make sure that the dates for rise and set are for the correct day this code checks the resultant rise and set days.
    // as all times are calculated using UTC there is a chance that they may not be for the requested local day.
    // I have attempted to adjust the passed in time using the local time offset however this has not worked, so a
    // brute force method has been applied to recalculate using either the next or previous day.
    // this method has also run into problems when adjusting by +/- 24 hours. It appears that borderline cases may actually
    // cause the calculation to jump another whole day and therefore result in a calculation that is another full day in the
    // desired direction. Currently it appears that using +/- 23 hours will fix this as the borderline cases are as result
    // of the few seconds/minutes difference in rise times each day. This needs to be monitored however and more thorough testing applied.
    JulianTransitDetails details = calcTransitDetails(date, location, altitude);
    JulianTransitDetails detailsAdj = null;
    if (!DateUtils.isSameDay(date, details.getRiseTime())) {
        detailsAdj = calcTransitDetails(DateUtils.addHours(date, date.after(date) ? 23 : -23), location,
                altitude);/*w w  w .j a v  a 2s . c  om*/
        details.setRise((detailsAdj.getJulianRise().julian() - details.getReferenceTime().julian()) * 24);
    }

    if (!DateUtils.isSameDay(date, details.getSetTime())) {
        detailsAdj = calcTransitDetails(DateUtils.addHours(date, date.after(date) ? 23 : -23), location,
                altitude);
        details.setSet((detailsAdj.getJulianSet().julian() - details.getReferenceTime().julian()) * 24);
    }

    assert DateUtils.isSameDay(details.getRiseTime(), details.getSetTime());
    return details;
}

From source file:com.gargoylesoftware.htmlunit.CacheTest.java

/**
 * @throws Exception if the test fails/*from   w w w.jav  a 2s  .co  m*/
 */
@Test
public void isCacheableContent() throws Exception {
    final Cache cache = new Cache();
    final Map<String, String> headers = new HashMap<>();
    final WebResponse response = new DummyWebResponse() {
        @Override
        public String getResponseHeaderValue(final String headerName) {
            return headers.get(headerName);
        }
    };

    assertFalse(cache.isCacheableContent(response));

    headers.put("Last-Modified", "Sun, 15 Jul 2007 20:46:27 GMT");
    assertTrue(cache.isCacheableContent(response));

    headers.put("Last-Modified", formatHttpDate(DateUtils.addMinutes(new Date(), -5)));
    assertFalse(cache.isCacheableContent(response));

    headers.put("Expires", formatHttpDate(DateUtils.addMinutes(new Date(), 5)));
    assertFalse(cache.isCacheableContent(response));

    headers.put("Expires", formatHttpDate(DateUtils.addHours(new Date(), 1)));
    assertTrue(cache.isCacheableContent(response));

    headers.remove("Last-Modified");
    assertTrue(cache.isCacheableContent(response));

    headers.put("Expires", "0");
    assertFalse(cache.isCacheableContent(response));

    headers.put("Expires", "-1");
    assertFalse(cache.isCacheableContent(response));
}