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.naver.blog.functionalservice.search.QueryParameter.java

private String calcStartDate(TermType termType, Date startDate) {
    if (startDate != null) {
        try {//from  w  w w  .  jav a 2s  .  c om
            Date truncatedStartDate = DateUtils.truncate(startDate, Calendar.DATE);
            return DateUtil.formatDate(truncatedStartDate, YYYYMMDDHHMM);
        } catch (Exception e) {
            Log.warn(e.getMessage(), e);
            return null;
        }
    }

    if (termType == null) {
        return null;
    }

    Calendar cal = Calendar.getInstance();
    switch (termType) {
    case IN_ONE_WEEK:
        cal.add(Calendar.DATE, -7);
        Date aWeekAgo = DateUtils.truncate(new Date(cal.getTimeInMillis()), Calendar.DATE);
        return DateUtil.formatDate(aWeekAgo, YYYYMMDDHHMM);
    case IN_ONE_MONTH:
        cal.add(Calendar.MONTH, -1);
        Date aMonthAgo = DateUtils.truncate(new Date(cal.getTimeInMillis()), Calendar.DATE);
        return DateUtil.formatDate(aMonthAgo, YYYYMMDDHHMM);
    default:
        return null;
    }
}

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

/**
 * Prueba para actualizar un Content//from  w ww  . j  a v  a2s . com
 *
 * @generated
 */
@Test
public void updateContentTest() throws IOException {
    Cookie cookieSessionId = login(username, password);
    ContentDTO content = new ContentDTO(oraculo.get(0));

    ContentDTO contentChanged = factory.manufacturePojo(ContentDTO.class);

    content.setName(contentChanged.getName());
    content.setContentValue(contentChanged.getContentValue());
    //Accuracy of dates is untiil day of month 
    Date dateChangedTruncated = DateUtils.truncate(content.getDate(), Calendar.DAY_OF_MONTH);
    content.setDate(dateChangedTruncated);

    Response response = target.path(content.getId().toString()).request().cookie(cookieSessionId)
            .put(Entity.entity(content, MediaType.APPLICATION_JSON));

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

    Assert.assertEquals(Ok, response.getStatus());
    Assert.assertEquals(content.getName(), contentTest.getName());
    Assert.assertEquals(content.getContentValue(), contentTest.getContentValue());
    Assert.assertEquals(content.getDate(), contentTest.getDate());
}

From source file:ch.algotrader.service.algo.VWAPOrderServiceTest.java

@Before
public void setup() {

    this.vwapOrderService = new VWAPOrderService(this.orderExecutionService, this.calendarService,
            this.simpleOrderService);
    this.vwapOrderService.setApplicationContext(this.applicationContext);

    Mockito.when(this.applicationContext.getBean(HistoricalDataService.class))
            .thenReturn(this.historicalDataService);

    this.exchange = Exchange.Factory.newInstance("TEST_EXCHANGE", TimeZone.getDefault().toString());
    this.exchange.setId(1);

    SecurityFamily family = SecurityFamily.Factory.newInstance("", Currency.USD, 1, 2, "0<0.01", true, false);
    family.setExchange(this.exchange);

    this.security = Stock.Factory.newInstance(family);
    this.security.setSymbol("TEST_SECURITY");
    this.security.setId(1);

    this.strategy = Strategy.Factory.newInstance("TEST_STRATEGY", false);

    this.historicalBars = new ArrayList<Bar>();
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 9, 0, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 9123));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 9, 15, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 7061));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 9, 30, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 7635));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 9, 45, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 6984));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 10, 0, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 8671));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 10, 15, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 7703));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 10, 30, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 6531));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 10, 45, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 8234));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 11, 0, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 5930));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 11, 15, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 8151));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 11, 30, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 6053));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 11, 45, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 7192));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 12, 0, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 6314));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 12, 15, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 6391));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 12, 30, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 5838));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 12, 45, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 6128));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 13, 0, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 6249));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 13, 15, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 7025));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 13, 30, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 5997));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 13, 45, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 6658));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 14, 0, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 6946));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 14, 15, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 4920));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 14, 30, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 5094));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 14, 45, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 7601));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 15, 0, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 7192));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 15, 15, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 7482));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 15, 30, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 7393));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 15, 45, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 6295));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 16, 0, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 5955));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 16, 15, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 8821));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 16, 30, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 7751));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 16, 45, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 7976));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 17, 0, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 9067));
    this.historicalBars.add(Bar.Factory.newInstance(DateUtil.dateForYMDHMS(2016, 1, 1, 17, 15, 0), null,
            this.security, Duration.MIN_15, null, null, null, null, 10569));

    Mockito.when(this.historicalDataService.getHistoricalBars(this.security.getId(), //
            DateUtils.truncate(new Date(), Calendar.DATE), //
            this.lookBackDays, //
            TimePeriod.DAY, //
            this.bucketSize, //
            MarketDataEventType.TRADES, //
            Collections.emptyMap())).thenReturn(this.historicalBars);

    Mockito.when(this.calendarService.isOpen(this.exchange.getId())).thenReturn(false);

    Mockito.when(this.calendarService.getNextOpenTime(this.exchange.getId()))
            .thenReturn(DateUtil.dateForYMDHMS(2016, 1, 1, 9, 00, 0));

    Mockito.when(this.calendarService.getNextCloseTime(this.exchange.getId()))
            .thenReturn(DateUtil.dateForYMDHMS(2016, 1, 1, 17, 30, 0));
}

From source file:com.ikanow.aleph2.v1.document_db.utils.LegacyV1HadoopUtils.java

/** C/P from v1 QueryHandler.parseMinMaxDates
 * @param time// ww  w.  j  a va2 s .co  m
 * @param nMinTime
 * @param nMaxTime
 * @param lockMaxToNow
 * @return
 */
public static Interval parseMinMaxDates(AdvancedQueryPojo.QueryTermPojo.TimeTermPojo time, long nMinTime,
        long nMaxTime, boolean lockMaxToNow) {

    if ((null != time.min) && (time.min.length() > 0)) {
        if (time.min.equals("now")) {
            nMinTime = nMaxTime;
        } else if (time.min.startsWith("now")) { // now+N[hdmy] 
            // now+Xi or now-Xi
            long sgn = 1L;
            if ('-' == time.min.charAt(3)) { //now+ or now-
                sgn = -1L;
            }
            long offset = sgn * getInterval(time.min.substring(4), 'd'); // (default interval is day)
            nMinTime = nMaxTime + offset; // (maxtime is now)
        } else if (time.min.equals("midnight")) {
            nMinTime = DateUtils.truncate(new Date(nMaxTime), Calendar.DAY_OF_MONTH).getTime();
        } else if (time.min.startsWith("midnight")) { // midnight+N[hdmy] 
            // midnight+Xi or midnight-Xi
            long sgn = 1L;
            if ('-' == time.min.charAt(8)) { //now+ or now-
                sgn = -1L;
            }
            long offset = sgn * getInterval(time.min.substring(9), 'd'); // (default interval is day)
            nMinTime = DateUtils.truncate(new Date(nMaxTime), Calendar.DAY_OF_MONTH).getTime() + offset; // (maxtime is now)
        } else {
            try {
                nMinTime = Long.parseLong(time.min); // (unix time format)
                if (nMinTime <= 99999999) { // More likely to be YYYYMMDD
                    // OK try a bunch of common date parsing formats
                    nMinTime = parseDate(time.min);
                } // TESTED for nMaxTime
            } catch (NumberFormatException e) {
                // OK try a bunch of common date parsing formats
                nMinTime = parseDate(time.min);
            }
        }
    }
    if ((null != time.max) && (time.max.length() > 0)) {
        if (time.max.equals("midnight")) {
            nMaxTime = DateUtils.truncate(new Date(nMaxTime), Calendar.DAY_OF_MONTH).getTime();
        } else if (time.max.startsWith("midnight")) { // midnight+N[hdmy] 
            // midnight+Xi or midnight-Xi
            long sgn = 1L;
            if ('-' == time.max.charAt(8)) { //now+ or now-
                sgn = -1L;
            }
            long offset = sgn * getInterval(time.min.substring(9), 'd'); // (default interval is day)
            nMaxTime = DateUtils.truncate(new Date(nMaxTime), Calendar.DAY_OF_MONTH).getTime() + offset; // (maxtime is now)
        } else if (!time.max.equals("now")) { // (What we have by default)
            if (time.max.startsWith("now")) { // now+N[hdmy] 
                // now+Xi or now-Xi
                long sgn = 1L;
                if ('-' == time.max.charAt(3)) { //now+ or now-
                    sgn = -1L;
                }
                long offset = sgn * getInterval(time.max.substring(4), 'd'); // (default interval is day)
                nMaxTime = nMaxTime + offset; // (maxtime is now)
            } else {
                try {
                    nMaxTime = Long.parseLong(time.max); // (unix time format)
                    if (nMaxTime <= 99999999) { // More likely to be YYYYMMDD
                        // OK try a bunch of common date parsing formats
                        nMaxTime = parseDate(time.max);

                        // max time, so should be 24h-1s ahead ...
                        nMaxTime = nMaxTime - (nMaxTime % (24 * 3600 * 1000));
                        nMaxTime += 24 * 3600 * 1000 - 1;

                    } //TESTED (logic5, logic10 for maxtime)
                } catch (NumberFormatException e) {
                    // OK try a bunch of common date parsing formats
                    nMaxTime = parseDate(time.max);

                    // If day only is specified, should be the entire day...
                    if (!time.max.contains(":")) {
                        nMaxTime = nMaxTime - (nMaxTime % (24 * 3600 * 1000));
                        nMaxTime += 24 * 3600 * 1000 - 1;
                    }
                } //TOTEST max time
            }
        }
    } //TESTED (logic5)

    if (lockMaxToNow) {
        if ((null == time.max) || time.max.isEmpty()) {
            nMinTime = new Date().getTime();
            nMaxTime = Long.MAX_VALUE;
        } else { // set max to now
            long now = new Date().getTime();
            if ((null != time.min) && !time.min.isEmpty()) { // set min also
                nMinTime = nMinTime + (now - nMaxTime);
            }
            nMaxTime = now;
        }
    } //TESTED (by hand)
    if (nMinTime > nMaxTime) { //swap these round if not ordered correctly
        long tmp = nMinTime;
        nMinTime = nMaxTime;
        nMaxTime = tmp;
    } //TESTED (by hand)

    return new Interval(nMinTime, nMaxTime);
}

From source file:com.marc.lastweek.web.pages.classifiedadslisting.FilterResultsPage.java

private boolean filterParametersHasResults(FilterParameters filterParameters) {
    DateTime now = new DateTime(Calendar.getInstance());
    Calendar date = DateUtils.truncate(now.minusWeeks(1).toCalendar(getLocale()), Calendar.DAY_OF_MONTH);
    return !LastweekApplication.get().getClassifiedAdsService()
            .findClassifiedAdsByFilterParameters(filterParameters, 0, 15, date).isEmpty();
}

From source file:net.bible.service.common.CommonUtils.java

public static Date getTruncatedDate() {
    return DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
}

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

/**
 * gets the holiday for this date//from  ww  w  .j a  v a 2  s .  c o  m
 */
private Holiday getHoliday(final Exchange exchange, final Date dateTime) {

    Validate.notNull(exchange, "exchange not found");
    Validate.notNull(dateTime, "dateTime is null");

    final Date date = DateUtils.truncate(dateTime, Calendar.DATE);

    return CollectionUtils.find(exchange.getHolidays(), new Predicate<Holiday>() {

        @Override
        public boolean evaluate(Holiday holiday) {
            return DateUtils.isSameDay(holiday.getDate(), date);
        }
    });
}

From source file:net.ravendb.client.RavenDBAwareTests.java

protected Date mkDate(int year, int month, int day) {
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    calendar = DateUtils.truncate(calendar, Calendar.DAY_OF_MONTH);
    calendar.set(year, month - 1, day);//w  w w .j a v a  2  s  .c  o  m
    return calendar.getTime();
}

From source file:com.manydesigns.portofino.pageactions.crud.CrudAction4ItsProject.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public void processExpiredOldDelete(Object row) {
    Map rowMap = (HashMap) row;
    Date t_time = (Date) rowMap.get("c_warranty_end");
    if (null != t_time) {
        rowMap.put("c_is_expired", t_time.before(DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH)));
    }//from w  w  w  .  java2 s  .co m
}

From source file:com.manydesigns.portofino.pageactions.crud.CrudAction4ItsProject.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public void processExpired(Object row) {
    Map rowMap = (HashMap) row;
    Date t_time = (Date) rowMap.get("c_warranty_end");
    if (null != t_time) {
        rowMap.put("c_is_expired", t_time.before(DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH)));
    }//from   ww w  . j av a  2s  .  c  o  m
}