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: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());
   }/*from   w  w w  . java2s  .  c o  m*/
}

From source file:MainClass.java

public static void main(String[] pArgs) throws Exception {
    Date now = new Date();
    Date truncYear = DateUtils.truncate(now, Calendar.YEAR);
    Date truncMonth = DateUtils.truncate(now, Calendar.MONTH);
    System.out.println("now: " + DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(now));
    System.out.println("truncYear: " + DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(truncYear));
    System.out.println("truncMonth: " + DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(truncMonth));
}

From source file:com.ocpsoft.socialpm.util.Dates.java

/**
 * Perform an inclusive date range comparison to a specific field precision
 * /*  w w w.j  av a  2 s. co m*/
 * @param field
 *            see <i>java.util.Calendar</i> Millisecond, Second, Minute,
 *            Hour, Day, Week, etc...
 */
public static boolean isInPrecisionRange(Date start, Date end, Date date, final int field) {
    start = DateUtils.truncate(start, field);
    end = DateUtils.truncate(end, field);
    date = DateUtils.truncate(date, field);
    if ((date.compareTo(start) >= 0) && (date.compareTo(end) <= 0)) {
        return true;
    }
    return false;
}

From source file:net.bible.service.readingplan.ReadingPlanInfoDto.java

public void setStartToJan1() {
    Date jan1 = DateUtils.truncate(new Date(), Calendar.YEAR);

    startOn(jan1, true);
}

From source file:com.clican.pluto.dataprocess.dpl.function.impl.DayOfMonth.java

public Object calculate(Map<String, Object> row) throws CalculationException, PrefixAndSuffixException {
    Date d = date.getValue(row);/*from  ww  w  . ja  va2 s  . c  om*/
    d = DateUtils.truncate(d, Calendar.MONTH);
    d = DateUtils.addMonths(d, 1);
    d = DateUtils.addDays(d, -1);
    SimpleDateFormat sdf = new SimpleDateFormat("dd");
    return Integer.parseInt(sdf.format(d));
}

From source file:com.microsoft.exchange.DateHelp.java

/**
 * Convert a {@link String} in the common date/time format for this application into a {@link Date}.
 * //w  w w  .  ja v  a2s . co  m
 * @param timePhrase format: "yyyyMMdd-HHmm"
 * @return the corresponding date
 * @throws IllegalArgumentException
 */
public static Date parseDateTimePhrase(final String timePhrase) {
    if (timePhrase == null) {
        return null;
    }
    try {
        Date time = getDateTimeFormat().parse(timePhrase);
        time = DateUtils.truncate(time, Calendar.MINUTE);
        return time;
    } catch (ParseException e) {
        throw new IllegalArgumentException("cannot parse date/time phrase " + timePhrase, e);
    }
}

From source file:fr.xebia.demo.objectgrid.ticketing.TrainSearchAgentTest.java

public void test() throws Exception {

    Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
    Date noonToday = DateUtils.addHours(today, 12);

    TrainSearchAgent trainSearchAgent = new TrainSearchAgent(PARIS_GARE_DE_LYON, noonToday,
            MARSEILLE_SAINT_CHARLES);//from w  ww  .  j a v a 2  s .  com

    Session session = objectGrid.getSession();
    ObjectMap employeeMap = session.getMap("Train");
    AgentManager agentManager = employeeMap.getAgentManager();

    List<RouteDetails> matchingTrainsByPartionId = (List<RouteDetails>) agentManager
            .callReduceAgent(trainSearchAgent);
    logger.debug(matchingTrainsByPartionId.size() + " matching trains found ");

    for (RouteDetails routeDetails : matchingTrainsByPartionId) {
        logger.debug(routeDetails);
    }
}

From source file:ar.com.zauber.commons.date.SignificantDateProviderTest.java

/** dia del mes */
@Test//from w w w  .  j av  a2  s.c o  m
public final void testDay() {
    final DateProvider provider = new SignificantDateProvider(target, Calendar.DAY_OF_MONTH);
    Assert.assertEquals(DateUtils.truncate(new Date(1269028059L * 1000), Calendar.DAY_OF_MONTH),
            provider.getDate());
}

From source file:com.microsoft.exchange.DateHelper.java

/**
 * //from   w ww  . j a v a 2 s.  c o m
 * @param value
 * @return
 */
public static Date makeDate(String value) {
    SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
    try {
        Date date = df.parse(value);
        return DateUtils.truncate(date, java.util.Calendar.DATE);
    } catch (ParseException e) {
        throw new IllegalArgumentException(value + " does not match expected format " + DATE_FORMAT, e);
    }
}

From source file:gov.utah.dts.det.ccl.model.FacilityTest.java

@Test
public void testGetLatestLicense() {
    Date now = DateUtils.truncate(new Date(), Calendar.DATE);
    Facility f1 = new Facility();

    //null should be returned when the facility has no license
    assertNull(f1.getLatestLicense());/*from w w w. j  av a2  s  . c  o  m*/

    //future licenses should not be returned
    License l2 = new License();
    l2.setStartDate(DateUtils.addDays(now, 30));
    l2.setExpirationDate(DateUtils.addDays(now, 60));

    //licenses that have expired should be returned
    License l3 = new License();
    l3.setStartDate(DateUtils.addDays(now, -60));
    l3.setExpirationDate(DateUtils.addDays(now, -30));
    f1.addLicense(l3);
    assertEquals(f1.getLatestLicense(), l3);

    //current licenses should be returned
    License l4 = new License();
    l4.setStartDate(DateUtils.addDays(now, -29));
    l4.setExpirationDate(DateUtils.addDays(now, 29));
    f1.addLicense(l4);
    assertEquals(f1.getLatestLicense(), l4);
}