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:edu.cornell.mannlib.vitro.webapp.controller.freemarker.DumpTestController.java

private Employee getEmployee() {

    Calendar c = Calendar.getInstance();
    c.set(1982, Calendar.MAY, 5);
    c = DateUtils.truncate(c, Calendar.DATE);
    Employee jdoe = new Employee("John", "Doe", 34523, c.getTime());
    jdoe.setFavoriteColors("blue", "green");
    jdoe.setSalary(65000);//  www . j a va 2  s.  c  om

    c.clear();
    c.set(1975, Calendar.OCTOBER, 25);
    c = DateUtils.truncate(c, Calendar.DATE);
    Employee jsmith = new Employee("Jane", "Smith", 78234, c.getTime());
    jsmith.setFavoriteColors("red", "orange");

    jdoe.setSupervisor(jsmith);

    return jdoe;
}

From source file:TripServlet.java

void filterTodayTrips() {
    Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
    filterdtrips.clear();//from  ww w  . jav  a 2  s  .c  o  m
    for (int i = 0; i < alltrips.size(); i++) {
        Date d = alltrips.get(i).start_Date;
        if (DateUtils.isSameDay(d, today)) {
            filterdtrips.add(alltrips.get(i));
        }
    }
}

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

/**
 * {@inheritDoc}//from  ww  w.  ja v  a  2s .  com
 */
@Override
public Date getCloseTime(final long exchangeId, final Date date) {

    Validate.notNull(date, "Date is null");

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

    Date dateTruncated = DateUtils.truncate(date, Calendar.DATE);
    TimeIntervals timeIntervals = getTimeIntervals(exchange, dateTruncated);
    return timeIntervals.isEmpty() ? null : timeIntervals.last().getTo();

}

From source file:ma.glasnost.orika.test.perf.MultiThreadedTestCase.java

@Test
@Concurrent(20)/*from   w  w w  .  j  a  va2 s . co  m*/
public void testGenerateObjectFactories() {

    Person person = new Person();
    person.setFirstName("Abdelkrim");
    person.setLastName("EL KHETTABI");
    Calendar cal = Calendar.getInstance();
    cal = DateUtils.truncate(cal, Calendar.DAY_OF_MONTH);
    cal.add(Calendar.YEAR, -31);
    person.setDateOfBirth(cal.getTime());
    person.setAge(31L);

    PersonVO vo = mapper.map(person, PersonVO.class);

    Assert.assertEquals(person.getFirstName(), vo.getFirstName());
    Assert.assertEquals(person.getLastName(), vo.getLastName());
    Assert.assertTrue(person.getAge() == vo.getAge());
    Assert.assertEquals(cal.getTime(), vo.getDateOfBirth());

    Person mapBack = mapper.map(vo, Person.class);
    Assert.assertEquals(person, mapBack);
}

From source file:br.com.nordestefomento.jrimum.utilix.DateUtil.java

/**
 * <p>/*  w w w. j  a  va  2 s . c om*/
 * Calcula a diferena de dias entre duas datas. O resultado  modular, ou
 * seja, maior ou igual a zero, logo a data final no precisa ser
 * necessariamente maior que a data inicial.
 * </p>
 * 
 * @param dataInicial
 *            - data inicial do intervalo.
 * @param dataFinal
 *            - data final do intervalo.
 * @return nmero(mdulo) de dias entre as datas.
 * 
 * @throws IllegalArgumentException
 *             Caso pelo menos uma das duas datas seja <code>null</code>.
 * @since 0.2
 */
public static long calculeDiferencaEmDias(final Date dataInicial, final Date dataFinal) {

    long fator = 0;
    Date dataInicialTruncada, dataFinalTruncada;

    if (isNotNull(dataInicial) && isNotNull(dataFinal)) {

        dataInicialTruncada = DateUtils.truncate(dataInicial, Calendar.DATE);
        dataFinalTruncada = DateUtils.truncate(dataFinal, Calendar.DATE);

        fator = ((dataFinalTruncada.getTime() - dataInicialTruncada.getTime()) / DateUtils.MILLIS_PER_DAY);

        if (fator < 0) {
            fator *= -1;
        }
    } else {
        throw new IllegalArgumentException("A data inicial [" + dataInicial + "] e a data final [" + dataFinal
                + "] " + "no podem ter valor 'null'.");
    }

    return fator;
}

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

/**
 * Prueba para crear un News//w  w  w .j ava  2s .  co m
 *
 * @throws java.io.IOException
 * @generated
 */
public void createNewsTest() throws IOException {
    NewsDTO news = factory.manufacturePojo(NewsDTO.class);
    //Accuracy of dates is untiil day of month 
    Date dateTruncated = DateUtils.truncate(news.getDate(), Calendar.DAY_OF_MONTH);
    news.setDate(dateTruncated);
    Cookie cookieSessionId = login(username, password);

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

    NewsDTO newsTest = (NewsDTO) response.readEntity(NewsDTO.class);

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

    Assert.assertEquals(news.getName(), newsTest.getName());
    Assert.assertEquals(news.getContent(), newsTest.getContent());
    Assert.assertEquals(news.getDate(), newsTest.getDate());

    NewsEntity entity = em.find(NewsEntity.class, newsTest.getId());
    Assert.assertNotNull(entity);
}

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

/**
 * Prueba para crear un Raiting//from   w  w w .  j  a  va2 s .  c o m
 *
 * @generated
 */
@Test
public void createRaitingTest() throws IOException {
    RaitingDTO raiting = factory.manufacturePojo(RaitingDTO.class);
    //Accuracy of dates is untiil day of month 
    Date dateTruncated = DateUtils.truncate(raiting.getDate(), Calendar.DAY_OF_MONTH);
    raiting.setDate(dateTruncated);
    Cookie cookieSessionId = login(username, password);

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

    RaitingDTO raitingTest = (RaitingDTO) response.readEntity(RaitingDTO.class);

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

    Assert.assertEquals(raiting.getName(), raitingTest.getName());
    Assert.assertEquals(raiting.getValue(), raitingTest.getValue());
    Assert.assertEquals(raiting.getDate(), raitingTest.getDate());
    Assert.assertEquals(raiting.getTextComment(), raitingTest.getTextComment());

    RaitingEntity entity = em.find(RaitingEntity.class, raitingTest.getId());
    Assert.assertNotNull(entity);
}

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

@Override
public Date getNextOpenTime(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.truncate(dateTime, Calendar.DATE);
    Date openTime;// ww w . jav  a  2s .  co  m
    NavigableSet<Date> openTimes = new TreeSet<>();
    while ((openTime = openTimes.ceiling(dateTime)) == null) {
        openTimes.addAll(getOpenTimes(exchange, date));
        date = DateUtils.addDays(date, 1);
    }
    return openTime;

}

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

@Test
public void testCreateLicensedFacility() {
    Facility f1 = new Facility();
    assertTrue(f1.getLicenses().isEmpty());

    FacilityDao mockedFd = mock(FacilityDao.class);
    when(mockedFd.save(f1)).thenReturn(f1);
    FacilityServiceImpl fsi = getNewFacilityServiceImpl(mockedFd);
    f1 = fsi.createLicensedFacility(f1, lSpecRegionOne);

    assertEquals(FacilityStatus.IN_PROCESS, f1.getStatus());
    assertFalse(f1.getLicenses().isEmpty());
    assertEquals(lSpecRegionOne, f1.getLicensingSpecialist());

    License lic = f1.getLicenses().iterator().next();
    assertEquals(f1, lic.getFacility());
    assertEquals(mockedInProcessType, lic.getSubtype());
    assertEquals(DateUtils.truncate(new Date(), Calendar.DATE), lic.getStartDate());
    assertEquals(License.DEFAULT_LICENSE_END_DATE, lic.getExpirationDate());
    assertFalse(lic.isCalculatesAlerts());
}

From source file:net.sf.housekeeper.swing.FoodEditorView.java

/**
 * Creates a JSpinner which uses the current locale's short format for
 * displaying a date. It's default date date is set to today's date.
 * /*from  w w w. ja v a  2 s  .  c o m*/
 * @return The created spinner.
 */
private JSpinner createDateSpinner() {
    final SpinnerDateModel model = new SpinnerDateModel();

    //Need to truncate the current date for correct spinner operation
    model.setStart(DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH));
    model.setCalendarField(Calendar.DAY_OF_MONTH);

    final JSpinner spinner = new JSpinner(model);

    //Set the spinner's editor to use the current locale's short date
    // format
    final SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance(DateFormat.SHORT);
    final String formatPattern = dateFormat.toPattern();
    spinner.setEditor(new JSpinner.DateEditor(spinner, formatPattern));

    return spinner;
}