Java Utililty Methods TimeUnit Usage

List of utility methods to do TimeUnit Usage

Description

The list of methods to do TimeUnit Usage are organized into topic(s).

Method

StringgetFormattedTime(long timeleft)
get Formatted Time
String formattedTime = null;
if (timeleft <= 0)
    formattedTime = String.format("%02d:%02d:%02d", 0, 0, 0);
else {
    final long hours = TimeUnit.MILLISECONDS.toHours(timeleft);
    final long minutes = TimeUnit.MILLISECONDS.toMinutes(timeleft - TimeUnit.HOURS.toMillis(hours));
    final long seconds = TimeUnit.MILLISECONDS
            .toSeconds(timeleft - TimeUnit.HOURS.toMillis(hours) - TimeUnit.MINUTES.toMillis(minutes));
...
StringgetFormattedTimeForMS(Long timeInMillis)
get Formatted Time For MS
TimeUnit timeunit = TimeUnit.MILLISECONDS;
long hours = timeunit.toHours(timeInMillis);
long minutes = timeunit.toMinutes(timeInMillis) - TimeUnit.HOURS.toMinutes(hours);
long seconds = timeunit.toSeconds(timeInMillis) - TimeUnit.HOURS.toSeconds(hours)
        - TimeUnit.MINUTES.toSeconds(minutes);
return ((hours < 10) ? "0" + hours : hours) + ":" + ((minutes < 10) ? "0" + minutes : minutes) + ":"
        + ((seconds < 10) ? "0" + seconds : seconds);
StringgetFriendlyTime(long duration)
Gets a human-readable String containing the time.
final StringBuilder sb = new StringBuilder();
long months = TimeUnit.MILLISECONDS.toDays(duration);
if (months >= 30) {
    months = months / 30;
    duration -= TimeUnit.DAYS.toMillis(months * 30);
} else {
    months = 0;
final long years = months / 12;
if (years > 0) {
    months -= Math.floor(years * 12);
    duration -= TimeUnit.DAYS.toMillis(years * 365);
final long days = TimeUnit.MILLISECONDS.toDays(duration);
duration -= TimeUnit.DAYS.toMillis(days);
final long hours = TimeUnit.MILLISECONDS.toHours(duration);
duration -= TimeUnit.HOURS.toMillis(hours);
final long minutes = TimeUnit.MILLISECONDS.toMinutes(duration);
duration -= TimeUnit.MINUTES.toMillis(minutes);
final long seconds = TimeUnit.MILLISECONDS.toSeconds(duration);
if (years > 0) {
    sb.append(" ").append(years);
    sb.append(" ").append(years == 1 ? "year" : "years");
if (months > 0) {
    sb.append(" ").append(months);
    sb.append(" ").append(months == 1 ? "month" : "months");
if (days > 0) {
    sb.append(" ").append(days);
    sb.append(" ").append(days == 1 ? "day" : "days");
if (hours > 0) {
    sb.append(" ").append(hours);
    sb.append(" ").append(hours == 1 ? "hour" : "hours");
if (minutes > 0) {
    sb.append(" ").append(minutes);
    sb.append(" ").append(minutes == 1 ? "minute" : "minutes");
if (seconds > 0) {
    sb.append(" ").append(seconds);
    sb.append(" ").append(seconds == 1 ? "second" : "seconds");
return sb.toString().trim();
intgetInsertionEndIndex(List list, Delayed key)
This function uses the binary search and adds a small amount of logic such that it determines the placement index for a given item.
return getInsertionEndIndex(list, key, list instanceof RandomAccess);
StringgetInStringSeconds(String pollInterval)
get In String Seconds
long pollIntervalInSeconds = (int) TimeUnit.MILLISECONDS.toSeconds(Long.parseLong(pollInterval));
return Long.toString(pollIntervalInSeconds);
longgetLastSlotDate(final int occurrences, final int periodicity, final long firstSlotDate, final int firstSlotDay, final String selectedDays)
get Last Slot Date
long lastSlotDate = firstSlotDate;
if (occurrences > 1) {
    int selectedDay = firstSlotDay;
    int intervalFromFirstDay = 0;
    for (int i = 1; i <= (occurrences - 1); i++) {
        int interval = getNextInterval(selectedDay, selectedDays, periodicity);
        intervalFromFirstDay += interval;
        selectedDay = (selectedDay + interval) % 7;
...
LonggetLongFromQueue(BlockingQueue queue)
get Long From Queue
Long nextValue = null;
boolean done = false;
while (done == false) {
    try {
        nextValue = queue.poll();
        while (nextValue == null) {
            Thread.sleep(Q_POLL_SLEEP_DURATION_);
            nextValue = queue.poll();
...
PropertiesgetMailServerProps(String toAddress, int smtpPort, int imapPort)
Sets up the properties in order to connect to the green mail test server.
Properties props = new Properties();
props.setProperty("connect.mail.user", toAddress);
props.setProperty("connect.mail.pass", toAddress);
props.setProperty("connect.max.msgs.in.batch", Integer.toString(MAX_NUM_MSGS_IN_BATCH));
props.setProperty("connect.delete.unhandled.msgs", "false");
props.setProperty("connect.mail.session.debug", "true");
props.setProperty("mail.smtp.host", "localhost");
props.setProperty("mail.smtp.auth", "TRUE");
...
intgetMinutes(final long milliseconds)
get Minutes
return (int) (milliseconds % HOUR / MINUTE);
intgetMinutesBetweenDates(Date begin_date, Date end_date)
Calculates duration between two Date objects.
return (int) TimeUnit.MILLISECONDS.toMinutes(end_date.getTime() - begin_date.getTime());