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:com.clican.pluto.dataprocess.dpl.function.SingleRowFunctionTestCase.java

private ProcessorContext getContext() throws Exception {
    // Fund/*  w  ww  .  ja  v a 2s. com*/
    List<BeanPrice> priceList1 = new ArrayList<BeanPrice>();
    List<BeanPrice> priceList2 = new ArrayList<BeanPrice>();
    List<Date> dateList = new ArrayList<Date>();
    double[] value1 = new double[] { 2, 4, 6, 7, 5, 3, 4, 3, 2, 1 };
    double[] value2 = new double[] { 2, 3, 4, 6, 2, 1, 5, 2, 1, 2 };
    Date current = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
    for (int i = 0; i < 10; i++) {
        BeanPrice price1 = new BeanPrice();
        price1.setId(i);
        price1.setCode(i + "");
        price1.setDate(DateUtils.add(current, Calendar.DAY_OF_MONTH, i - 10));
        BeanPrice price2 = new BeanPrice();
        price2.setId(i);
        price2.setDate(DateUtils.add(current, Calendar.DAY_OF_MONTH, i - 10));
        price1.setPrice(value1[i]);
        price2.setPrice(value2[i]);
        priceList1.add(price1);
        priceList2.add(price2);
        dateList.add(DateUtils.add(current, Calendar.DAY_OF_MONTH, i - 10));
    }

    ProcessorContext context = new ProcessorContextImpl();
    context.setAttribute("priceList1", priceList1);
    context.setAttribute("priceList2", priceList2);
    context.setAttribute("dateList", dateList);
    return context;
}

From source file:com.comcast.cats.service.util.VideoRecorderUtil.java

/**
 * Truncate hour to the next hour./*w  w w. jav  a  2  s  .co m*/
 * 
 * @param now
 * @return
 */
public static Date getScheduledStartTime(Date now) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(DateUtils.truncate(now, Calendar.HOUR_OF_DAY));
    calendar.add(Calendar.HOUR_OF_DAY, 1);
    Date scheduledStartTime = calendar.getTime();

    LOGGER.info("Calculated scheduledStartTime [" + scheduledStartTime + "]");

    return scheduledStartTime;
}

From source file:co.edu.uniandes.csw.turism.tests.rest.ContentTest.java

/**
 * Prueba para crear un Content//  w  ww .j a  v  a2s .co  m
 *
 * @generated
 */
@Test
public void createContentTest() throws IOException {
    ContentDTO content = factory.manufacturePojo(ContentDTO.class);

    //Accuracy of dates is untiil day of month 
    Date dateTruncated = DateUtils.truncate(content.getDate(), Calendar.DAY_OF_MONTH);
    content.setDate(dateTruncated);
    Cookie cookieSessionId = login(username, password);

    Response response = target.request().cookie(cookieSessionId)
            .post(Entity.entity(content, MediaType.APPLICATION_JSON));

    ContentDTO contentTest = (ContentDTO) response.readEntity(ContentDTO.class);

    Assert.assertEquals(Created, response.getStatus());

    Assert.assertEquals(content.getName(), contentTest.getName());
    Assert.assertEquals(content.getDate(), contentTest.getDate());
    Assert.assertEquals(content.getContentValue(), contentTest.getContentValue());

    ContentEntity entity = em.find(ContentEntity.class, contentTest.getId());
    Assert.assertNotNull(entity);
}

From source file:ch.algotrader.service.CalendarServiceImpl.java

@Override
public Date getNextCloseTime(final long exchangeId, final Date dateTime) {

    Validate.notNull(dateTime, "DateTime is null");

    Exchange exchange = this.exchangeDao.get(exchangeId);
    Validate.notNull(exchange, "exchange not found");

    Date date = DateUtils.addDays(DateUtils.truncate(dateTime, Calendar.DATE), -1);
    Date closeTime;/*  w  w w  .jav a  2s  .  c o  m*/
    NavigableSet<Date> closeTimes = new TreeSet<>();
    while ((closeTime = closeTimes.ceiling(dateTime)) == null) {
        closeTimes.addAll(getCloseTimes(exchange, date));
        date = DateUtils.addDays(date, 1);
    }
    return closeTime;

}

From source file:co.edu.uniandes.csw.turism.tests.rest.NewsTest.java

/**
 * Prueba para consultar un FAQ/*www  . ja v  a2s. c o m*/
 *
 * @generated
 */
@Test
public void getNewsByIdTest() {
    Cookie cookieSessionId = login(username, password);

    NewsDTO newsTest = target.path(oraculo.get(0).getId().toString()).request().cookie(cookieSessionId)
            .get(NewsDTO.class);

    Date dateTruncated = DateUtils.truncate(oraculo.get(0).getDate(), Calendar.DAY_OF_MONTH);

    Assert.assertEquals(newsTest.getId(), oraculo.get(0).getId());
    Assert.assertEquals(newsTest.getName(), oraculo.get(0).getName());
    Assert.assertEquals(newsTest.getContent(), oraculo.get(0).getContent());
    Assert.assertEquals(newsTest.getDate(), dateTruncated);
}

From source file:com.surevine.alfresco.repo.action.delete.PurgeDeletedItemsJob.java

/**
 * Separated into a separate method to allow for optimisation etc.  
 * @return//from  ww  w  .j  a  v  a2  s  . c o m
 */
protected String assembleLuceneQuery(final int purgeDays) {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 0 - purgeDays);
    cal = DateUtils.truncate(cal, Calendar.DAY_OF_MONTH);
    String luceneDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(cal.getTime());

    String query = "ASPECT:\"md:deleted\"" + " AND (@md\\:deletedTimestamp:[MIN TO " + luceneDate + "]"
            + " OR (ISNULL:\"md:deletedTimestamp\"" + " AND @cm\\:modified:[MIN TO " + luceneDate + "]" + ")"
            + ")";

    if (_logger.isDebugEnabled()) {
        _logger.debug(query);
    }

    return query;
}

From source file:com.swordlord.gozer.datatypeformat.DataTypeHelper.java

/**
 * @param dataTypeClass//from w  ww .  java  2s .  c  om
 * @param untypedValue
 * @return
 */
public static Object toDataType(Class<?> dataTypeClass, Object untypedValue) {
    if ((dataTypeClass == null) || (untypedValue == null)
            || ClassUtils.isAssignable(untypedValue.getClass(), dataTypeClass)) {
        if (Date.class == dataTypeClass) {
            return DateUtils.truncate(untypedValue, Calendar.DATE);
        }

        return untypedValue;
    }

    Object v = null;

    String strUntypedValue = null;
    boolean isStringUntypedValue = untypedValue instanceof String;

    Number numUntypedValue = null;
    boolean isNumberUntypedValue = untypedValue instanceof Number;

    if (isStringUntypedValue) {
        strUntypedValue = (String) untypedValue;
    }

    if (isNumberUntypedValue) {
        numUntypedValue = (Number) untypedValue;
    }

    if (dataTypeClass == boolean.class || dataTypeClass == Boolean.class) {
        if (isNumberUntypedValue) {
            v = BooleanUtils.toBooleanObject(numUntypedValue.intValue());
        } else if (isStringUntypedValue) {
            v = BooleanUtils.toBooleanObject(strUntypedValue);
        }
    } else if (dataTypeClass == Integer.class) {
        if (isNumberUntypedValue) {
            v = new Integer(numUntypedValue.intValue());
        } else if (isStringUntypedValue) {
            v = NumberUtils.createInteger(strUntypedValue);
        }
    } else if (dataTypeClass == Double.class) {
        if (isNumberUntypedValue) {
            v = new Double(numUntypedValue.doubleValue());
        } else if (isStringUntypedValue) {
            v = NumberUtils.createDouble(strUntypedValue);
        }
    } else if (dataTypeClass == Date.class) {
        if (isNumberUntypedValue) {
            v = DateUtils.truncate(new Date(numUntypedValue.longValue()), Calendar.DATE);
        }
    } else {
        v = ObjectUtils.toString(untypedValue);
    }

    return v;
}

From source file:gov.utah.dts.det.ccl.service.impl.FacilityServiceImpl.java

@Override
public Facility createLicensedFacility(Facility facility, Person licensingSpecialist)
        throws CclServiceException {
    if (facility == null) {
        throw new IllegalArgumentException("Facility must not be null");
    }/*from   ww w .j ava  2  s . c  o  m*/
    if (licensingSpecialist == null) {
        throw new IllegalArgumentException("Licensing specialist must not be null");
    }

    setLicensingSpecialist(facility, licensingSpecialist);

    if (facility.getRegion() == null) {
        throw new CclServiceException("Unable to create a new licensed facility",
                "The system was unable to assign a region to the facility because the licensor is not assigned to a region.");
    }

    facility.setStatus(FacilityStatus.IN_PROCESS);

    // get the license status for in process
    PickListValue licenseStatusInProcess = applicationService
            .getPickListValueForApplicationProperty(ApplicationPropertyKey.IN_PROCESS_LICENSE_STATUS.getKey());

    // create the license
    License license = new License();
    license.setStatus(licenseStatusInProcess);

    // set the license start date as today
    license.setStartDate(DateUtils.truncate(new Date(), Calendar.DATE));

    // set the license end date far in the future
    license.setExpirationDate(License.DEFAULT_LICENSE_END_DATE);

    license.setCalculatesAlerts(false);

    facility.addLicense(license);

    facility = facilityDao.save(facility);
    setFacilityId(facility);

    return facility;
}

From source file:gov.nih.nci.cabig.ctms.audit.dao.AuditHistoryRepository.java

/**
 * Gets the list of data audit events for a domain object.
 *
 * @param entityClass the entity class/*  ww w  .j av  a  2 s  .  c o  m*/
 * @param entityId    the entity id
 * @param calendar    the calendar
 * @return the list of data audit events
 */
@SuppressWarnings("unchecked")
private List<DataAuditEvent> getDataAuditEvents(final Class entityClass, final Integer entityId,
        final Calendar calendar) {

    DataAuditEventQuery dataAuditEventQuery = new DataAuditEventQuery();
    dataAuditEventQuery.leftJoinFetch("e.values");

    dataAuditEventQuery.filterByClassName(entityClass.getName());
    dataAuditEventQuery.filterByEntityId(entityId);

    if (calendar != null) {
        final Calendar newCalendar = (Calendar) calendar.clone();
        newCalendar.add(Calendar.DAY_OF_MONTH, +1);
        Date startDate = DateUtils.truncate(calendar.getTime(), Calendar.DATE);
        Date endDate = DateUtils.truncate(newCalendar.getTime(), Calendar.DATE);
        dataAuditEventQuery.filterByStartDateAfter(startDate);
        dataAuditEventQuery.filterByEndDateBefore(endDate);

    }
    final List<DataAuditEvent> dataAuditEvents = auditHistoryDao.findDataAuditEvents(dataAuditEventQuery);

    return dataAuditEvents;

}

From source file:gov.nih.nci.firebird.data.AbstractCredential.java

static boolean isExpired(Date expirationDate) {
    if (expirationDate == null) {
        return false;
    }//  w  w w . j  a v a2 s. c o m
    Date today = DateUtils.truncate(new Date(), Calendar.MONTH);
    Date expires = DateUtils.truncate(expirationDate, Calendar.MONTH);
    return expires.before(today);
}