Example usage for org.apache.commons.lang.time DurationFormatUtils formatDuration

List of usage examples for org.apache.commons.lang.time DurationFormatUtils formatDuration

Introduction

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

Prototype

public static String formatDuration(long durationMillis, String format) 

Source Link

Document

Formats the time gap as a string, using the specified format, and padding with zeros and using the default timezone.

This method formats durations using the days and lower fields of the format pattern.

Usage

From source file:gov.nih.nci.cabig.caaers.utils.DurationUtils.java

public static String formatDuration(long actualDuration, String format) {
    String msgPrefix = "Due in ";
    String msgSuffix = "overdue";
    if (actualDuration < 0) {
        msgPrefix = "";
    } else {/*from   w  ww . j av  a  2s. com*/
        msgSuffix = "";
    }

    long duration = Math.abs(actualDuration);

    int years = (format.contains("y")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "y"))
            : 0;
    duration -= (years * YEAR);
    int months = (format.contains("M")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "M"))
            : 0;
    duration -= (months * MONTH);
    int days = (format.contains("d")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "d")) : 0;
    duration -= (days * DAY);
    int hours = (format.contains("H")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "H"))
            : 0;
    duration -= (hours * HOUR);
    int minutes = (format.contains("m")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "m"))
            : 0;
    duration -= (minutes * MINUTE);
    int seconds = (format.contains("s")) ? Integer.parseInt(DurationFormatUtils.formatDuration(duration, "s"))
            : 0;

    //apply rounding to days / Hours / minutes
    if (format.contains("d") && format.contains("H")) {
        if (hours > 12) {
            days += 1;
        }
        hours = 0;
    }

    //apply rounding of hours
    if (format.contains("m") && format.contains("H")) {
        if (minutes > 30) {
            hours += 1;
        }
        minutes = 0;
    }

    //apply rounding of minutes
    if (format.contains("m") && format.contains("s")) {
        if (seconds > 30) {
            minutes += 1;
        }
        seconds = 0;
    }

    StringBuffer sb = new StringBuffer(msgPrefix);
    if (years > 0) {
        sb.append(years).append(years > 1 ? " years" : " year").append(" ");
    }
    if (months > 0) {
        sb.append(months).append(months > 1 ? " months" : " month").append(" ");
    }
    if (days > 0) {
        sb.append(days).append(days > 1 ? " days" : " day").append(" ");
    }
    if (hours > 0) {
        sb.append(hours).append(hours > 1 ? " hours" : " hour").append(" ");
    }
    if (minutes > 0) {
        sb.append(minutes).append(minutes > 1 ? " minutes" : " minute").append(" ");
    }
    if (seconds > 0) {
        sb.append(seconds).append(seconds > 1 ? " seconds" : " second").append(" ");
    }
    sb.append(msgSuffix);
    return sb.toString();

}

From source file:au.id.hazelwood.xmltvguidebuilder.utils.DateTimeUtils.java

public static String formatDurationDHM(long duration) {
    return DurationFormatUtils.formatDuration(duration, "dd'd'HH'h'mm'm'");
}

From source file:com.microsoftopentechnologies.windowsazurestorage.service.StoragePluginService.java

protected String getTime(long timeInMills) {
    return DurationFormatUtils.formatDuration(timeInMills, "HH:mm:ss.S") + " (HH:mm:ss.S)";
}

From source file:eionet.cr.dto.HarvestDTO.java

/**
 * Gets the duration string.//from ww  w .  j a va2 s.co m
 *
 * @return the duration string
 */
public String getDurationString() {
    if (datetimeStarted == null) {
        return WebConstants.NOT_AVAILABLE;
    }
    if (datetimeFinished == null) {
        Date now = new Date();
        return DurationFormatUtils.formatDuration(now.getTime() - datetimeStarted.getTime(), "HH:mm:ss");
    }
    return DurationFormatUtils.formatDuration(datetimeFinished.getTime() - datetimeStarted.getTime(),
            "HH:mm:ss");
}

From source file:com.edgenius.wiki.quartz.RemoveSpaceJobInvoker.java

public int getLeftHours(String spaceUname) {
    try {//from  w w w .ja v  a  2  s . c  o m
        Trigger trigger = scheduler
                .getTrigger(new TriggerKey(REMOVE_SPACE_TRIGGER + spaceUname, QUARTZ_EXPORTABLE_JOB_GROUP));
        if (trigger != null) {
            String hours = DurationFormatUtils
                    .formatDuration(trigger.getStartTime().getTime() - new Date().getTime(), "H");
            return NumberUtils.toInt(hours);
        } else {
            log.warn("Unable to get job detail for space remove job " + spaceUname);
        }
    } catch (SchedulerException e) {
        log.error("Unable to get job detail for space remove job " + spaceUname, e);
    }
    return 0;
}

From source file:com.ms.app.web.commons.tools.DateViewTools.java

/**
 * <p>/*from  w ww.j a  v  a 2s  .c o  m*/
 * Duration formatting utilities and constants. The following table describes the tokens used in the pattern
 * language for formatting.
 * </p>
 * <table border="1">
 * <tr>
 * <th>character</th>
 * <th>duration element</th>
 * </tr>
 * <tr>
 * <td>y</td>
 * <td>years</td>
 * </tr>
 * <tr>
 * <td>M</td>
 * <td>months</td>
 * </tr>
 * <tr>
 * <td>d</td>
 * <td>days</td>
 * </tr>
 * <tr>
 * <td>H</td>
 * <td>hours</td>
 * </tr>
 * <tr>
 * <td>m</td>
 * <td>minutes</td>
 * </tr>
 * <tr>
 * <td>s</td>
 * <td>seconds</td>
 * </tr>
 * <tr>
 * <td>S</td>
 * <td>milliseconds</td>
 * </tr>
 * </table>
 */
public static String formatDuration(Date start, Date end, String format) {
    if (start == null || end == null) {
        return " ";
    }
    long durationMillis = end.getTime() - start.getTime();
    return DurationFormatUtils.formatDuration(durationMillis, format);
}

From source file:com.linkedin.drelephant.mapreduce.heuristics.GenericSkewHeuristic.java

private String convertTimeMs(long timeMs) {
    if (timeMs < 1000) {
        return Long.toString(timeMs) + " msec";
    }//from   w w  w .j a v a  2 s .  co m
    return DurationFormatUtils.formatDuration(timeMs, "HH:mm:ss") + " HH:MM:SS";
}

From source file:de.tudarmstadt.ukp.dkpro.tc.crfsuite.task.CRFSuiteTestTask.java

private String trainModel(TaskContext aContext) throws Exception {
    String tmpModelLocation = System.getProperty("java.io.tmpdir") + File.separator + MODELNAME;
    List<String> modelTrainCommand = buildTrainCommand(aContext, tmpModelLocation);

    log("Start training model");
    long time = System.currentTimeMillis();
    runTrain(modelTrainCommand);//from   w w w.  j a  v a 2  s .c o  m
    long completedIn = System.currentTimeMillis() - time;
    String formattedDuration = DurationFormatUtils.formatDuration(completedIn, "HH:mm:ss:SS");
    log("Training finished after " + formattedDuration);

    return writeModel(aContext, tmpModelLocation);
}

From source file:de.tudarmstadt.ukp.dkpro.tc.svmhmm.task.SVMHMMTestTask.java

/**
 * Trains the model and stores it into the task context
 *
 * @param taskContext context//w w w. j  a  va2s . co  m
 * @throws Exception
 */
protected void trainModel(TaskContext taskContext, File trainingFile) throws Exception {

    File tmpModelFile = File.createTempFile("tmp_svm_hmm", ".model");

    // we have to copy the training file to tmp to prevent long path issue in svm_hmm
    File tmpTrainingFile = File.createTempFile("tmp_svm_hmm_training", ".txt");
    FileUtils.copyFile(trainingFile, tmpTrainingFile);

    List<String> modelTrainCommand = buildTrainCommand(tmpTrainingFile, tmpModelFile.getPath());

    log.debug("Start training model");
    long time = System.currentTimeMillis();
    runCommand(modelTrainCommand);
    long completedIn = System.currentTimeMillis() - time;
    String formattedDuration = DurationFormatUtils.formatDuration(completedIn, "HH:mm:ss:SS");
    log.info("Training finished after " + formattedDuration);

    FileInputStream stream = new FileInputStream(tmpModelFile);
    taskContext.storeBinary(MODEL_NAME, stream);

    // clean-up
    IOUtils.closeQuietly(stream);
    FileUtils.deleteQuietly(tmpModelFile);
    FileUtils.deleteQuietly(tmpTrainingFile);
}

From source file:com.fengduo.bee.commons.util.DateViewTools.java

public static String formatDuration(Date start, Date end, String format) {
    if (start == null || end == null) {
        return " ";
    }//from   w w  w.  j  a  v  a  2s.  c o  m

    long durationMillis = end.getTime() - start.getTime();

    return DurationFormatUtils.formatDuration(durationMillis, format);

}