Example usage for org.apache.commons.lang.time DateFormatUtils format

List of usage examples for org.apache.commons.lang.time DateFormatUtils format

Introduction

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

Prototype

public static String format(Date date, String pattern) 

Source Link

Document

Formats a date/time into a specific pattern.

Usage

From source file:org.talend.metadata.managment.model.MDMConnectionFillerImpl.java

private String getTechXSDFolderName() {
    String techXSDFolderName = MetadataConnectionUtils.createTechnicalName(
            DatabaseConstant.XSD_SUFIX + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss")); //$NON-NLS-1$
    IFolder xsdFolder = ReponsitoryContextBridge.getRootProject()
            .getFolder(new Path("metadata/MDMconnections")); //$NON-NLS-1$
    // ResourceManager.getMDMConnectionFolder().getFolder(DatabaseConstant.XSD_SUFIX);
    if (xsdFolder.getFolder(techXSDFolderName).exists()) {
        try {//from  www  .  ja va2 s . co  m
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return getTechXSDFolderName();
    }
    return techXSDFolderName;
}

From source file:org.thiesen.ant.git.ExtractGitInfo.java

private void exportProperties(final GitInfo info, final Project currentProject) {
    currentProject.setProperty(prefixName("branch"), info.getCurrentBranch());
    currentProject.setProperty(prefixName("workingcopy.dirty"), String.valueOf(info.isWorkingCopyDirty()));
    currentProject.setProperty(prefixName("commit"), info.getLastCommit());
    currentProject.setProperty(prefixName("commit.short"), info.getLastCommitShort());
    currentProject.setProperty(prefixName("commit.date"),
            DateFormatUtils.format(info.getLastCommitDate(), "EEE, dd MMM yyyy HH:mm:ss Z"));
    currentProject.setProperty(prefixName("tag"), info.getLastTagName());
    currentProject.setProperty(prefixName("tag.hash"), info.getLastTagHash());
    currentProject.setProperty(prefixName("tag.dirty"), String.valueOf(info.isLastTagDirty()));
    currentProject.setProperty(prefixName("tag.author.name"), info.getLastTagAuthorName());
    currentProject.setProperty(prefixName("tag.author.email"), info.getLastTagAuthorEmail());
    currentProject.setProperty(prefixName("dirty"),
            String.valueOf(info.isWorkingCopyDirty() || info.isLastTagDirty()));
    currentProject.setProperty(prefixName("version"), info.getVersionPostfix());
}

From source file:org.tsm.concharto.util.TimeRangeFormat.java

/** 
 * format a date/*from   w  w  w  .ja v  a2s .c  o m*/
 * @param date date
 * @param cp CalendarPrecision for getting one of the format strings
 * @return String formatted string
 */
private static String dateFormat(Date date, CalendarPrecision cp) {
    String format;
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);
    //if the year is an AD date and it is pretty old (e.g. less than 1000AD), then append the era
    //always display the era for BC dates
    if ((cal.get(Calendar.ERA) == GregorianCalendar.BC) || (cal.get(Calendar.YEAR) < MAX_YEAR_TO_DISLPAY_ERA)) {
        format = cp.getFormatWithEra();
    } else {
        format = cp.getFormat();
    }

    String text = DateFormatUtils.format(date, format);
    return stripLeadingZeros(date, text);
}

From source file:org.vpac.ndg.storage.util.TimeSliceUtil.java

/**
 * Find appropriate units for the time dimension.
 * //  w  w w. j  a va2s  .  c om
 * @param timeSlices
 *            The time slices to find a unit for.
 * @param dates
 *            A list of coordinates in the new units, which map 1:1 to the
 *            given time slices. This should be an empty list; it will be
 *            populated by this method.
 * @return A description the new units.
 */
public CalendarDateUnit computeTimeMapping(List<TimeSlice> timeSlices, List<CalendarDate> dates) {

    // We need to know the smallest precision of the selected time slices,
    // so that a sensible value can be used in the exported NCML: the
    // coordinates will use units like "Days since 2011-01-01". See:
    // http://www.unidata.ucar.edu/software/netcdf/docs/BestPractices.html#Calendar%20Date/Time
    // http://www.unidata.ucar.edu/software/netcdf/time/recs.html

    Date mindate = null;
    long minprecision = Long.MAX_VALUE;
    for (TimeSlice ts : timeSlices) {
        if (mindate == null || ts.getCreated().before(mindate))
            mindate = ts.getCreated();
        Dataset ds = timeSliceDao.getParentDataset(ts.getId());
        if (minprecision == Long.MAX_VALUE || ds.getPrecision() < minprecision)
            minprecision = ds.getPrecision();
    }

    // Find an epoch: truncate the date to the nearest sensible major value,
    // e.g. to the nearest year.
    Date epoch;
    String timeUnits;

    if (minprecision < DateUtils.MILLIS_PER_SECOND) {
        // Precision of less than one second; round to milliseconds.
        epoch = DateUtils.truncate(mindate, Calendar.SECOND);
        timeUnits = String.format("msec since %s", DateFormatUtils.format(epoch, Default.SECOND_PATTERN));

    } else if (minprecision < DateUtils.MILLIS_PER_MINUTE) {
        // Precision of less than one minute; round to seconds.
        epoch = DateUtils.truncate(mindate, Calendar.MINUTE);
        timeUnits = String.format("Seconds since %s", DateFormatUtils.format(epoch, Default.MINUTE_PATTERN));

    } else if (minprecision < DateUtils.MILLIS_PER_HOUR) {
        // Precision of less than one hour; round to minutes.
        epoch = DateUtils.truncate(mindate, Calendar.HOUR);
        timeUnits = String.format("Minutes since %s", DateFormatUtils.format(epoch, Default.HOUR_PATTERN));

    } else if (minprecision < DateUtils.MILLIS_PER_DAY) {
        // Precision of less than one day; round to hours.
        epoch = DateUtils.truncate(mindate, Calendar.DAY_OF_MONTH);
        timeUnits = String.format("Hours since %s", DateFormatUtils.format(epoch, Default.DAY_PATTERN));

    } else {
        // Precision GREATER than one day; round to days.
        epoch = DateUtils.truncate(mindate, Calendar.YEAR);
        timeUnits = String.format("Days since %s", DateFormatUtils.format(epoch, Default.DAY_PATTERN));
    }

    CalendarDateUnit units = CalendarDateUnit.of("proleptic_gregorian", timeUnits);

    // Calculate a set of new coordinates for each time slice, relative to
    // the epoch.
    for (TimeSlice ts : timeSlices) {
        CalendarDate coordinate = CalendarDate.of(ts.getCreated().getTime());
        dates.add(coordinate);
    }

    return units;
}

From source file:org.xaloon.core.api.asynchronous.RetryAction.java

/**
 * @param parameters/* ww  w .j  a  va2 s  . co m*/
 * @return expected result type
 * @throws InterruptedException
 */
public T perform(Z parameters) throws InterruptedException {
    int i = 0;
    T result = null;
    int timeToSleep = millisecondsToSleep;
    if (randomTimeUsed) {
        timeToSleep = new Random().nextInt(timeToSleep) + timeToSleep;
    }
    while (result == null && i++ < retryCount) {
        if (sleepFirst) {
            if (LOGGER.isWarnEnabled()) {
                LOGGER.warn(String.format("[%s]: [%d] Sleeping for %s s(%s)", Thread.currentThread().getName(),
                        i, DateFormatUtils.format(timeToSleep, "ss:SSS"), parameters));
            }
            Thread.sleep(timeToSleep);
        }
        try {
            result = onPerform(parameters);
        } catch (Exception e) {
            if (exceptionErrorLevel) {
                LOGGER.error("Action thrown an exception!", e);
            } else {
                LOGGER.debug("Action thrown an exception!", e);
            }
        }
        if (result == null && !sleepFirst) {
            if (LOGGER.isWarnEnabled()) {
                LOGGER.warn(String.format("[%s]: [%d] Sleeping for %s s(%s)", Thread.currentThread().getName(),
                        i, DateFormatUtils.format(timeToSleep, "ss:SSS"), parameters));
            }
            Thread.sleep(millisecondsToSleep);
        }
    }
    return result;
}

From source file:org.xmatthew.spy2servers.jmx.AbstractComponentViewMBean.java

public String getStartupDate() {
    if (getComponent() == null || getComponent().getStartupDate() == null) {
        return "";
    }/*  w w w.  j a  v a  2 s  .  c  o  m*/
    return DateFormatUtils.format(getComponent().getStartupDate(), DateConstant.FULL_DATE_PATTER);
}

From source file:org.xmlactions.mapping.bean_to_xml.PopulatorFromTimestamp.java

public Element performElementAction(List<KeyValue> keyvalues, BeanToXml beanToXml, Element parent,
        Object object, String namespacePrefix, String elementName, String beanRef) {
    if (object instanceof Timestamp) {
        Timestamp timestamp = (Timestamp) object;
        String output;/*from w  ww  .  j  av  a 2s . c  om*/
        String format = getKeyValue(keyvalues, MappingConstants.TIME_FORMAT);
        if (StringUtils.isNotEmpty(format)) {
            output = DateFormatUtils.format(timestamp.getTime(), format);
        } else {
            output = timestamp.toString();
        }
        Element element = BeanToXmlUtils.addElement(parent, namespacePrefix, elementName);
        element.setText(output);
        return element;
    } else {
        throw new IllegalArgumentException("The object parameter must be a " + Timestamp.class.getName()
                + " not [" + object.getClass().getName() + "]");
    }
}

From source file:org.xmlactions.mapping.bean_to_xml.PopulatorFromTimestamp.java

public Element performAttributeAction(List<KeyValue> keyvalues, BeanToXml beanToXml, Element parent,
        Object object, String namespacePrefix, String attributeName, String beanRef) {
    if (object instanceof Timestamp) {
        Timestamp timestamp = (Timestamp) object;
        String output;//from   ww  w  .  j  a v a  2s  .c om
        String format = getKeyValue(keyvalues, MappingConstants.TIME_FORMAT);
        if (StringUtils.isNotEmpty(format)) {
            output = DateFormatUtils.format(timestamp.getTime(), format);
        } else {
            output = timestamp.toString();
        }
        Element attribute = BeanToXmlUtils.addAttribute(parent, namespacePrefix, attributeName, output);
        return attribute;
    } else {
        throw new IllegalArgumentException("The object parameter must be a " + Timestamp.class.getName()
                + " not [" + object.getClass().getName() + "]");
    }
}

From source file:org.xmlactions.web.conceal.CreateHandyParams.java

private void addDate() {
    String formatter = execContext.getString(FORMATTER_DATE);
    if (StringUtils.isEmpty(formatter)) {
        formatter = XSD_DATE_FMT;/* ww  w. ja va 2 s .  com*/
    }
    Date date = new Date(System.currentTimeMillis());
    String d = DateFormatUtils.format(date, formatter);
    execContext.put(CURRENT_DATE, d);
}

From source file:org.xmlactions.web.conceal.CreateHandyParams.java

private void addTime() {
    String formatter = execContext.getString(FORMATTER_TIME);
    if (StringUtils.isEmpty(formatter)) {
        formatter = XSD_TIME_FMT;//from   w ww.ja va  2 s.co m
    }
    Date date = new Date(System.currentTimeMillis());
    String t = DateFormatUtils.format(date, formatter);
    execContext.put(CURRENT_TIME, t);
}