Example usage for org.apache.commons.lang.time DateUtils round

List of usage examples for org.apache.commons.lang.time DateUtils round

Introduction

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

Prototype

public static Date round(Object date, int field) 

Source Link

Document

Round 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 this was passed with HOUR, it would return 28 Mar 2002 14:00:00.000.

Usage

From source file:DateUtilsV1.java

 public static void main(String args[]) {
   GregorianCalendar calendar = new GregorianCalendar(1974, 5, 25, 6, 30, 30);
   Date date = calendar.getTime();

   System.err.println("Original Date: " + date);
   System.err.println("Rounded Date: " + DateUtils.round(date, Calendar.HOUR));
   System.err.println("Truncated Date: " +   DateUtils.truncate(date, Calendar.MONTH));

   Iterator itr = DateUtils.iterator(date, DateUtils.RANGE_WEEK_MONDAY);

   while(itr.hasNext()) {
      System.err.println(((Calendar)itr.next()).getTime());
   }// ww w. j  av  a 2 s  .c  om
}

From source file:MainClass.java

public static void main(String[] pArgs) throws Exception {

    Calendar cal = Calendar.getInstance();
    cal.set(2004, 2, 5, 10, 2, 2);//from   ww w.j a  v  a 2  s  .  c o  m

    System.out.println(DateUtils.round(cal.getTime(), DateUtils.SEMI_MONTH));

}

From source file:de.codesourcery.planning.DateAxisTestTool.java

public static void main(String[] args) {

    axis.setStartDate(DateUtils.round(new Date(), Calendar.DAY_OF_MONTH));
    axis.setRange(Duration.weeks(1));
    axis.setTickDuration(Duration.oneDay());

    JFrame frame = new JFrame();
    frame.getContentPane().add(new DateCanvas());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);/*from  w w  w  .  j  a  v a 2 s . c om*/
    frame.pack();
    frame.setVisible(true);
}

From source file:com.digitalpebble.stormcrawler.persistence.DefaultSchedulerTest.java

@Test
public void testScheduler() throws MalformedURLException {
    Map<String, Object> stormConf = new HashMap<>();
    stormConf.put("fetchInterval.testKey=someValue", 3600);
    DefaultScheduler scheduler = new DefaultScheduler();
    scheduler.init(stormConf);//from w w  w  . j  a  v  a2 s. c om

    Metadata metadata = new Metadata();
    metadata.addValue("testKey", "someValue");
    Date nextFetch = scheduler.schedule(Status.FETCHED, metadata);

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, 3600);
    Assert.assertEquals(DateUtils.round(cal.getTime(), Calendar.SECOND),
            DateUtils.round(nextFetch, Calendar.SECOND));
}

From source file:com.discursive.jccook.lang.DateFormatExample.java

public void testRoundedDates() throws Exception {
    FastDateFormat sdf = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT;
    Date now = new Date();
    Date nearestHour = DateUtils.round(now, Calendar.HOUR);
    Date nearestMonth = DateUtils.round(now, Calendar.MONTH);
    Date nearestYear = DateUtils.round(now, Calendar.YEAR);
    logger.debug("now: " + sdf.format(now));
    logger.debug("round hour: " + sdf.format(nearestHour));
    logger.debug("round month: " + sdf.format(nearestMonth));
    logger.debug("round year: " + sdf.format(nearestYear));
}

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

public void persist(final TickVO event, final Map<?, ?> map) throws IOException {

    Tick tick = Tick.Factory.newInstance();

    tick.setSecurity(this.lookupService.getSecurity(event.getSecurityId()));
    tick.setFeedType(event.getFeedType());
    tick.setLast(event.getLast());//from  ww w .  j  a  v  a 2s  .com
    tick.setLastDateTime(event.getLastDateTime());
    tick.setVol(event.getVol());
    tick.setAsk(event.getAsk());
    tick.setVolAsk(event.getVolAsk());
    tick.setBid(event.getBid());
    tick.setVolBid(event.getVolBid());

    // get the current Date rounded to MINUTES
    Date date = DateUtils.round(serverEngine.getCurrentTime(), Calendar.MINUTE);
    tick.setDateTime(date);

    this.marketDataPersistenceService.persistTick(tick);
}

From source file:ezbake.data.elastic.test.TestUtils.java

public static Facet generateDateBucketFacet() {
    final SimpleDateFormat dtg = new SimpleDateFormat("ddHHmm'Z' MM yy");
    final Calendar calendar = new GregorianCalendar();
    final Facet ssrDateFacet = new Facet();
    final RangeFacet dateRangeFacet = new RangeFacet();
    final BaseFacetValue dateField = new BaseFacetValue();
    dateField.setFacetField("visit");
    dateRangeFacet.setField(dateField);//from   w  w w . j av  a  2s  .com

    final FacetRange last24 = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -1);
    last24time = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    last24.setFrom(dtg.format(last24time));
    dateRangeFacet.addToRanges(last24);

    final FacetRange last48 = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -1);
    last48time = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    last48.setFrom(dtg.format(last48time));
    dateRangeFacet.addToRanges(last48);

    final FacetRange last72 = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -1);
    last72time = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    last72.setFrom(dtg.format(last72time));
    dateRangeFacet.addToRanges(last72);

    final FacetRange lastWeek = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -4);
    lastWeekTime = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    lastWeek.setFrom(dtg.format(lastWeekTime));
    dateRangeFacet.addToRanges(lastWeek);

    final FacetRange last30Days = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -23);
    last30DaysTime = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    last30Days.setFrom(dtg.format(last30DaysTime));
    dateRangeFacet.addToRanges(last30Days);

    final FacetRange last90Days = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -60);
    last90DaysTime = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    last90Days.setFrom(dtg.format(last90DaysTime));
    dateRangeFacet.addToRanges(last90Days);

    final FacetRange lastYear = new FacetRange(RangeType.DATE);
    calendar.add(Calendar.DAY_OF_YEAR, -275);
    lastYearTime = DateUtils.round(calendar, Calendar.HOUR).getTimeInMillis();
    lastYear.setFrom(dtg.format(lastYearTime));
    dateRangeFacet.addToRanges(lastYear);

    final FacetRequest dateRequest = new FacetRequest();
    dateRequest.setRangeFacet(dateRangeFacet);

    ssrDateFacet.setLabel("Report Date");
    ssrDateFacet.setFacet(dateRequest);

    return ssrDateFacet;
}

From source file:de.hybris.platform.commercefacades.product.converters.populator.ProductPromotionsPopulatorTest.java

@Test
public void testPopulate() {
    final ProductModel source = mock(ProductModel.class);
    final PromotionGroupModel defaultPromotionGroup = mock(PromotionGroupModel.class);
    final Date currentDate = DateUtils.round(new Date(), Calendar.MINUTE);
    final ProductPromotionModel productPromotionModel = mock(ProductPromotionModel.class);
    final PromotionData promotionData = mock(PromotionData.class);
    final BaseSiteModel baseSiteModel = mock(BaseSiteModel.class);

    given(timeService.getCurrentTime()).willReturn(currentDate);
    given(baseSiteService.getCurrentBaseSite()).willReturn(baseSiteModel);
    given(baseSiteModel.getDefaultPromotionGroup()).willReturn(defaultPromotionGroup);
    given(promotionsService.getProductPromotions(Collections.singletonList(defaultPromotionGroup), source, true,
            currentDate)).willReturn(Collections.singletonList(productPromotionModel));
    given(promotionsConverter.convert(productPromotionModel)).willReturn(promotionData);

    final ProductData result = new ProductData();
    productPromotionsPopulator.populate(source, result);

    Assert.assertEquals(1, result.getPotentialPromotions().size());
    Assert.assertEquals(promotionData, result.getPotentialPromotions().iterator().next());
}

From source file:de.hybris.platform.commercefacades.product.converters.populator.ProductPromotionsPopulator.java

@Override
public void populate(final SOURCE productModel, final TARGET productData) throws ConversionException {
    final BaseSiteModel baseSiteModel = getBaseSiteService().getCurrentBaseSite();
    if (baseSiteModel != null) {
        final PromotionGroupModel defaultPromotionGroup = baseSiteModel.getDefaultPromotionGroup();
        final Date currentTimeRoundedToMinute = DateUtils.round(getTimeService().getCurrentTime(),
                Calendar.MINUTE);

        if (defaultPromotionGroup != null) {
            final List<ProductPromotionModel> promotions = getPromotionsService().getProductPromotions(
                    Collections.singletonList(defaultPromotionGroup), productModel, true,
                    currentTimeRoundedToMinute);

            productData.setPotentialPromotions(Converters.convertAll(promotions, getPromotionsConverter()));
        }// w  ww  . j a  v a  2  s  .c om
    }
}

From source file:com.linuxbox.enkive.imap.mongo.MongoImapAccountCreator.java

public void createImapAccount(String username) throws MessageSearchException {
    HashMap<String, String> mailboxTable = new HashMap<String, String>();

    Date earliestMessageDate = searchService.getEarliestMessageDate(username);
    Date latestMessageDate = searchService.getLatestMessageDate(username);

    Calendar latestMessageCalendar = Calendar.getInstance();
    latestMessageCalendar.setTime(latestMessageDate);
    DateUtils.round(latestMessageCalendar, Calendar.DATE);
    Calendar today = Calendar.getInstance();
    DateUtils.round(today, Calendar.DATE);
    if (latestMessageCalendar.equals(today)) {
        latestMessageCalendar.add(Calendar.DATE, -1);
        latestMessageDate = latestMessageCalendar.getTime();
    }/* www  . ja  v  a 2 s.c  om*/

    // Setup Trash and Inbox
    BasicDBObject inboxObject = new BasicDBObject();
    String inboxPath = "INBOX";
    inboxObject.put(MongoEnkiveImapConstants.MESSAGEIDS, new HashMap<String, String>());
    imapCollection.insert(inboxObject);
    ObjectId inboxId = (ObjectId) inboxObject.get("_id");
    mailboxTable.put(inboxPath, inboxId.toString());

    BasicDBObject trashObject = new BasicDBObject();
    String trashPath = "Trash";
    trashObject.put(MongoEnkiveImapConstants.MESSAGEIDS, new HashMap<String, String>());
    imapCollection.insert(trashObject);
    ObjectId trashId = (ObjectId) inboxObject.get("_id");
    mailboxTable.put(trashPath, trashId.toString());

    BasicDBObject rootMailboxObject = new BasicDBObject();
    imapCollection.insert(rootMailboxObject);
    ObjectId id = (ObjectId) rootMailboxObject.get("_id");
    BasicDBObject userMailboxesObject = new BasicDBObject();
    userMailboxesObject.put(MongoEnkiveImapConstants.USER, username);
    mailboxTable.put(MongoEnkiveImapConstants.ARCHIVEDMESSAGESFOLDERNAME, id.toString());

    userMailboxesObject.put(MongoEnkiveImapConstants.MAILBOXES, mailboxTable);
    imapCollection.insert(userMailboxesObject);

    addImapMessages(username, earliestMessageDate, latestMessageDate);
}