Example usage for java.util.concurrent TimeUnit HOURS

List of usage examples for java.util.concurrent TimeUnit HOURS

Introduction

In this page you can find the example usage for java.util.concurrent TimeUnit HOURS.

Prototype

TimeUnit HOURS

To view the source code for java.util.concurrent TimeUnit HOURS.

Click Source Link

Document

Time unit representing sixty minutes.

Usage

From source file:Main.java

public static String getHumanReadableTime(long ms) {
    final long days = TimeUnit.MILLISECONDS.toDays(ms);
    ms -= TimeUnit.DAYS.toMillis(days);
    final long hours = TimeUnit.MILLISECONDS.toHours(ms);
    ms -= TimeUnit.HOURS.toMillis(hours);
    final long minutes = TimeUnit.MILLISECONDS.toMinutes(ms);
    ms -= TimeUnit.MINUTES.toMillis(minutes);
    final long seconds = TimeUnit.MILLISECONDS.toSeconds(ms);

    final StringBuilder sb = new StringBuilder();
    sb.append(days);//from   www  .  j av  a 2 s.c  o m
    sb.append("d ");
    sb.append(hours);
    sb.append("h ");
    sb.append(minutes);
    sb.append("m ");
    sb.append(seconds);
    sb.append('s');

    return (sb.toString());
}

From source file:Main.java

public static long getUpdateInterval(@NonNull final String intervalFromPreference) {
    final int parsedValue = Integer.valueOf(intervalFromPreference);
    return TimeUnit.MILLISECONDS.convert(parsedValue, TimeUnit.HOURS);
}

From source file:Main.java

public static String getTimeSpanFromSeconds(long secs) {
    if (TimeUnit.SECONDS.toHours(secs) == 0)
        return String.format("%dmin", TimeUnit.SECONDS.toMinutes(secs));
    else/* ww w. j a v  a2 s  . c  o  m*/
        return String.format("%dh %02dmin", TimeUnit.SECONDS.toHours(secs),
                TimeUnit.SECONDS.toMinutes(secs) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(secs)));
}

From source file:Main.java

/**
 * Builds String output of time in the style of: HH:MM:SS
 * @param millis/*from  w ww. j  av  a 2  s.  c o m*/
 * @return
 */
public static String buildTimerStyleTime(long millis) {
    return String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
            TimeUnit.MILLISECONDS.toMinutes(millis)
                    - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
            TimeUnit.MILLISECONDS.toSeconds(millis)
                    - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))

    );
}

From source file:Main.java

public static TimeUnit convert(ChronoUnit tu) {
    if (tu == null) {
        return null;
    }//w w  w  .j  a  va 2 s. c  om
    switch (tu) {
    case DAYS:
        return TimeUnit.DAYS;
    case HOURS:
        return TimeUnit.HOURS;
    case MINUTES:
        return TimeUnit.MINUTES;
    case SECONDS:
        return TimeUnit.SECONDS;
    case MICROS:
        return TimeUnit.MICROSECONDS;
    case MILLIS:
        return TimeUnit.MILLISECONDS;
    case NANOS:
        return TimeUnit.NANOSECONDS;
    default:
        //TODO support the rest
        throw new UnsupportedOperationException("Man, use a real temporal unit");
    }
}

From source file:Main.java

public static String getTimespentString(long millis) {

    if (millis == 0) {
        return "Undetermined";
    }//from   w  w w  . j  av  a2 s.  c  om

    StringBuilder sb = new StringBuilder(64);

    if (millis < 0) {
        sb.append("-");
        millis *= -1;
    }

    long hours = TimeUnit.MILLISECONDS.toHours(millis);
    millis -= TimeUnit.HOURS.toMillis(hours);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
    millis -= TimeUnit.MINUTES.toMillis(minutes);
    long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

    sb.append(hours);
    sb.append(":");

    if (minutes < 10) {
        sb.append("0");
    }
    sb.append(minutes);

    return (sb.toString());
}

From source file:Main.java

/**
 * millisecondsToHours//from  w ww.j  a  v  a  2 s.  c  o  m
 * Get the hh:mm value from milliseconds
 * @param millis the milliseconds value
 * @return a string with the time in hh:mm format
 */
public static String millisecondsToHours(long millis) {
    return String.format(Locale.getDefault(), "%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
            TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1));
}

From source file:Main.java

/**
 * Convert a millisecond duration to a string format
 *
 * @param millis A duration to convert to a string form
 * @return A string of the form "X Days Y Hours Z Minutes A Seconds".
 *///from  ww w  .j  a  v  a2s.c om
public static String getDurationBreakdown(long millis) {
    if (millis < 0) {
        throw new IllegalArgumentException("Duration must be greater than zero!");
    }

    long days = TimeUnit.MILLISECONDS.toDays(millis);
    millis -= TimeUnit.DAYS.toMillis(days);
    long hours = TimeUnit.MILLISECONDS.toHours(millis);
    millis -= TimeUnit.HOURS.toMillis(hours);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
    millis -= TimeUnit.MINUTES.toMillis(minutes);
    long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

    return (String.valueOf(days) + " Days " + hours + " Hours " + minutes + " Minutes " + seconds + " Seconds");
}

From source file:Main.java

/**
 * Convert a millisecond duration to a string format
 *
 * @param millis A duration to convert to a string form
 * @return A string of the form "X Days Y Hours Z Minutes A Seconds".
 *//*from ww w. j  a v  a  2  s . c  om*/
public static String getDurationBreakdownNoDays(long millis) {
    if (millis < 0) {
        throw new IllegalArgumentException("Duration must be greater than zero!");
    }

    long days = TimeUnit.MILLISECONDS.toDays(millis);
    millis -= TimeUnit.DAYS.toMillis(days);
    long hours = TimeUnit.MILLISECONDS.toHours(millis);
    millis -= TimeUnit.HOURS.toMillis(hours);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
    millis -= TimeUnit.MINUTES.toMillis(minutes);
    long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

    return (hours + " Hours " + minutes + " Minutes " + seconds + " Seconds");
}

From source file:Main.java

private static long hoursToMillis(long hours) {
    return TimeUnit.HOURS.toMillis(hours);
}