Example usage for org.apache.commons.lang3.time DateUtils truncate

List of usage examples for org.apache.commons.lang3.time DateUtils truncate

Introduction

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

Prototype

public static Date truncate(final Object date, final int field) 

Source Link

Document

Truncates a date, leaving the field specified as the most significant field.

For example, if you had the date-time 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:org.apache.lens.cube.metadata.UpdatePeriod.java

public Date truncate(Date date) {
    switch (this) {
    case WEEKLY://from w w  w . j  a  va  2  s. c  om
        Date truncDate = DateUtils.truncate(date, Calendar.DAY_OF_MONTH);
        Calendar cal = Calendar.getInstance();
        cal.setTime(truncDate);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        return cal.getTime();
    case QUARTERLY:
        Date dt = DateUtils.truncate(date, this.calendarField());
        dt.setMonth(dt.getMonth() - (dt.getMonth() % 3));
        return dt;
    default:
        return DateUtils.truncate(date, this.calendarField());
    }
}

From source file:org.cloud.mblog.utils.DateFormatTag.java

private String getOffsetTime(String newTime) {
    try {/*from  ww w .  ja  v a 2 s  .  c  o  m*/
        Date lastPost = DateUtils.parseDate(newTime, "yyyy-MM-dd HH:mm:ss.SSS");
        Date now = new Date();
        long duration = (now.getTime() - lastPost.getTime()) / 1000;
        if (duration < 60) {
            return duration + "?";
        } else if (duration < 3600) {
            return duration / 60 + "?";
        } else if (duration < 86400) {
            return duration / 3600 + "??";
        } else {
            Date zeroTime = DateUtils.truncate(now, Calendar.DATE);
            long offset_day = (zeroTime.getTime() - lastPost.getTime()) / 1000 / 3600 / 24;
            if (offset_day < 1) {
                return "";
            } else if (offset_day < 2) {
                return "?";
            } else if (offset_day < 365) {
                return offset_day + "?";
            } else {
                return offset_day / 365 + "?";
            }
        }
    } catch (ParseException e) {
        logger.error("error", e);
    }
    return "";
}

From source file:org.faster.util.DateTimes.java

public static Date parseStringToDateAndTruncateToMinute(String timeString) {
    Date date = parseStringToDate(timeString);
    return DateUtils.truncate(date, Calendar.MINUTE);
}

From source file:org.faster.util.DateTimes.java

public static Date parseStringToDateAndTruncateToHour(String timeString) {
    Date date = parseStringToDate(timeString);
    return DateUtils.truncate(date, Calendar.HOUR_OF_DAY);
}

From source file:org.faster.util.DateTimes.java

public static Date parseStringToDateAndTruncateToDate(String timeString) {
    Date date = parseStringToDate(timeString);
    return DateUtils.truncate(date, Calendar.DAY_OF_MONTH);
}

From source file:org.faster.util.DateTimes.java

public static Date buildDateNextHours(int n) {
    Calendar calTime = Calendar.getInstance();
    calTime.add(Calendar.HOUR_OF_DAY, n);
    return DateUtils.truncate(calTime.getTime(), Calendar.HOUR_OF_DAY);
}

From source file:org.finra.herd.core.HerdDateUtils.java

/**
 * Removes time portion of the date time object.
 *
 * @param xmlGregorianCalendar the xmlGregorianCalendar, not null
 *
 * @return the new {@code Timestamp} with reset time portion
 *///w  w  w  .ja  v  a2  s .c  o  m
public static Timestamp resetTimeToMidnight(XMLGregorianCalendar xmlGregorianCalendar) {
    Date date = xmlGregorianCalendar.toGregorianCalendar().getTime();
    return new Timestamp(DateUtils.truncate(date, Calendar.DATE).getTime());
}

From source file:org.inheritsource.service.common.domain.Timeline.java

public TreeMap<Date, List<TimelineItem>> getTimelineByDay() {
    TreeMap<Date, List<TimelineItem>> timelineByDay = new TreeMap<Date, List<TimelineItem>>(
            new Comparator<Date>() {
                @Override/*from  w w w. j ava2  s . co m*/
                public int compare(Date o1, Date o2) {
                    int result = 0;
                    if (o1 == null) {
                        if (o2 == null) {
                            return 0;
                        } else {
                            result = 1;
                        }
                    } else {
                        if (o2 == null) {
                            return -1;
                        } else {
                            result = -o1.compareTo(o2);
                        }
                    }
                    return result;
                }
            });

    if (!sorted) {
        sort();
    }

    Date keyDay = null;
    List<TimelineItem> dayItems = null;
    if (items != null) {
        for (TimelineItem item : items) {
            if (item != null) {
                if (item.getTimestamp() == null) {
                    log.error("Timestamp in TimelineItem is not expected to be null: " + item);
                } else {
                    if (keyDay == null || !DateUtils.isSameDay(keyDay, item.getTimestamp())) {
                        // new day in Map => create new day in map and a new list for that day
                        keyDay = DateUtils.truncate(item.getTimestamp(), Calendar.DAY_OF_MONTH);
                        dayItems = new ArrayList<TimelineItem>();
                        timelineByDay.put(keyDay, dayItems);
                    }
                    // always add item to current day in map
                    if (dayItems != null) {
                        dayItems.add(item);
                    } else {
                        log.error("dayItems should never be null on add");
                    }
                }
            }
        }
    }

    return timelineByDay;
}

From source file:org.kalypso.simulation.ui.ant.GmlPropertyTask.java

private void addProperty(final GMLWorkspace workspace, final Property property) {
    // validate/*  w  ww . j  a va 2 s .  c  o m*/
    final String name = property.getName();
    if (name == null || name.length() == 0)
        throw new BuildException("Property 'name' is not set.");

    final String featureProperty = property.getFeatureProperty();
    if (featureProperty == null || featureProperty.length() == 0)
        throw new BuildException("Property 'featureProperty' is not set.");

    final String featureID = property.getFeatureID();
    final String featurePath = property.getFeaturePath();
    if ((featureID == null || featureID.length() == 0) && featurePath == null)
        throw new BuildException("Neither 'featureID' nor 'featurePath' is set.");

    // find feature
    final Feature f;
    if (featureID != null) {
        f = workspace.getFeature(featureID);
        if (f == null)
            throw new BuildException("No feature for id: " + featureID);
    } else {
        final Object featureFromPath = workspace.getFeatureFromPath(featurePath);
        if (featureFromPath instanceof Feature)
            f = (Feature) featureFromPath;
        else
            throw new BuildException("No feature found with path: " + featurePath);
    }

    final Object value = f.getProperty(featureProperty);
    if (value instanceof XMLGregorianCalendar) {
        // special handling for Date
        // We write Date as String in XML-Format, in order to correctly transfer timezone information
        final Date date = DateUtilities.toDate(value);
        Calendar cal = Calendar.getInstance(KalypsoCorePlugin.getDefault().getTimeZone());
        cal.setTime(date);

        //      Calendar cal = ((XMLGregorianCalendar) value).toGregorianCalendar();
        //      cal.setTimeZone( KalypsoCorePlugin.getDefault().getTimeZone() );
        final Integer dateoffset = property.getDateoffset();
        final String dateoffsetfield = property.getDateoffsetfield();
        final String dateTruncField = property.getDateTruncField();

        if (dateoffset != null && dateoffsetfield != null)
            cal.add(CalendarUtilities.getCalendarField(dateoffsetfield), dateoffset.intValue());

        if (dateTruncField != null)
            cal = DateUtils.truncate(cal, Integer.parseInt(dateTruncField));

        final String dateString = DatatypeConverter.printDateTime(cal);
        m_propertyAdder.addProperty(name, dateString, null);
    } else if (value != null)
        m_propertyAdder.addProperty(name, value.toString(), null);
    else {
        final Project project2 = getProject();
        if (project2 != null)
            project2.log("No value for feature with id " + f.getId() + " in property: " + featureProperty,
                    Project.MSG_DEBUG);
        final String defaultValue = property.getDefaultValue();
        if (defaultValue != null) {
            if (project2 != null)
                project2.log("Using defualt value: " + defaultValue, Project.MSG_DEBUG);
            m_propertyAdder.addProperty(name, defaultValue, null);
        }
    }
}

From source file:org.paxml.el.UtilFunctions.java

/**
 * Get today./*from  ww w .  j av  a  2s .c  o m*/
 * 
 * @return today
 */
public static Date today() {
    return DateUtils.truncate(new Date(), Calendar.DATE);
}