Java Code Examples for java.util.concurrent.TimeUnit#toMinutes()

The following examples show how to use java.util.concurrent.TimeUnit#toMinutes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GrpcUtil.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Override
public String toAsciiString(Long timeoutNanos) {
  long cutoff = 100000000;
  TimeUnit unit = TimeUnit.NANOSECONDS;
  if (timeoutNanos < 0) {
    throw new IllegalArgumentException("Timeout too small");
  } else if (timeoutNanos < cutoff) {
    return timeoutNanos + "n";
  } else if (timeoutNanos < cutoff * 1000L) {
    return unit.toMicros(timeoutNanos) + "u";
  } else if (timeoutNanos < cutoff * 1000L * 1000L) {
    return unit.toMillis(timeoutNanos) + "m";
  } else if (timeoutNanos < cutoff * 1000L * 1000L * 1000L) {
    return unit.toSeconds(timeoutNanos) + "S";
  } else if (timeoutNanos < cutoff * 1000L * 1000L * 1000L * 60L) {
    return unit.toMinutes(timeoutNanos) + "M";
  } else {
    return unit.toHours(timeoutNanos) + "H";
  }
}
 
Example 2
Source File: GrpcUtil.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
public String toAsciiString(Long timeoutNanos) {
  long cutoff = 100000000;
  TimeUnit unit = TimeUnit.NANOSECONDS;
  if (timeoutNanos < 0) {
    throw new IllegalArgumentException("Timeout too small");
  } else if (timeoutNanos < cutoff) {
    return timeoutNanos + "n";
  } else if (timeoutNanos < cutoff * 1000L) {
    return unit.toMicros(timeoutNanos) + "u";
  } else if (timeoutNanos < cutoff * 1000L * 1000L) {
    return unit.toMillis(timeoutNanos) + "m";
  } else if (timeoutNanos < cutoff * 1000L * 1000L * 1000L) {
    return unit.toSeconds(timeoutNanos) + "S";
  } else if (timeoutNanos < cutoff * 1000L * 1000L * 1000L * 60L) {
    return unit.toMinutes(timeoutNanos) + "M";
  } else {
    return unit.toHours(timeoutNanos) + "H";
  }
}
 
Example 3
Source File: ForceOpDialog.java    From Wurst7 with GNU General Public License v3.0 5 votes vote down vote up
private String getTimeString(long ms)
{
	TimeUnit uDays = TimeUnit.DAYS;
	TimeUnit uHours = TimeUnit.HOURS;
	TimeUnit uMin = TimeUnit.MINUTES;
	TimeUnit uMS = TimeUnit.MILLISECONDS;
	
	long days = uMS.toDays(ms);
	long hours = uMS.toHours(ms) - uDays.toHours(days);
	long minutes = uMS.toMinutes(ms) - uHours.toMinutes(uMS.toHours(ms));
	long seconds = uMS.toSeconds(ms) - uMin.toSeconds(uMS.toMinutes(ms));
	
	return days + "d " + hours + "h " + minutes + "m " + seconds + "s";
}
 
Example 4
Source File: MLTimer.java    From RecSys2018 with Apache License 2.0 5 votes vote down vote up
private static String formatSeconds(double secondsF) {
	if (secondsF < 0) {
		return Double.toString(secondsF);
	}
	TimeUnit base = TimeUnit.SECONDS;
	int s = (int) Math.floor(secondsF);
	// float remainder = (float) (secondsF - s);

	long days = base.toDays(s);
	s -= TimeUnit.DAYS.toSeconds(days);
	long hours = base.toHours(s);
	s -= TimeUnit.HOURS.toSeconds(hours);
	long minutes = base.toMinutes(s);
	s -= TimeUnit.MINUTES.toSeconds(minutes);
	long secondsL = base.toSeconds(s);

	StringBuilder sb = new StringBuilder();
	if (days > 0) {
		sb.append(days);
		sb.append(" days ");
	}
	if (hours > 0 || days > 0) {
		sb.append(hours);
		sb.append(" hr ");
	}
	if (hours > 0 || days > 0 || minutes > 0) {
		sb.append(minutes);
		sb.append(" min ");
	}
	sb.append(secondsL + " sec");

	return sb.toString();
}
 
Example 5
Source File: RareManager.java    From Kepler with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Get the credit amount handout but in hours.
 *
 * @param hours the hours to select for
 * @return the amount of rareCost
 */
public int getHandoutAmountInHours(int hours) {
    TimeUnit unit = TimeUnit.valueOf(GameConfiguration.getInstance().getString("credits.scheduler.timeunit"));
    long interval = unit.toMinutes(GameConfiguration.getInstance().getInteger("credits.scheduler.interval"));

    long minutesInHour = 60;
    long minutes = minutesInHour / interval;

    return (int) ((hours * minutes) * GameConfiguration.getInstance().getInteger("credits.scheduler.amount"));
}
 
Example 6
Source File: DurationUtils.java    From ClockPlus with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a breakdown of a given time into its values
 * in hours, minutes, seconds and milliseconds.
 * @param t the time to break down
 * @param unit the {@link TimeUnit} the given time is expressed in
 * @param roundMillis whether rounding of milliseconds is desired
 * @return a {@code long[]} of the values in hours, minutes, seconds
 *         and milliseconds in that order
 */
public static long[] breakdown(long t, @NonNull TimeUnit unit, boolean roundMillis) {
    long days = unit.toDays(t);
    long hours = unit.toHours(t) % 24;
    long minutes = unit.toMinutes(t) % 60;
    long seconds = unit.toSeconds(t) % 60;
    long msecs = unit.toMillis(t) % 1000;
    if (roundMillis) {
        if (msecs >= 500) {
            seconds++;
            msecs = 0;
            if (seconds == 60) {
                minutes++;
                seconds = 0;
                if (minutes == 60) {
                    hours++;
                    minutes = 0;
                    if (hours == 24) {
                        days++;
                        hours = 0;
                    }
                }
            }
        }
    }
    return new long[] { days, hours, minutes, seconds, msecs };
}
 
Example 7
Source File: LogTimeFormatter.java    From clust4j with Apache License 2.0 5 votes vote down vote up
private static TimeSlots fromTimeUnit(long amt, final TimeUnit unit) {
	final long hr = unit.toHours(amt); 
	amt -= subtractAmt(hr, unit, TimeUnit.HOURS);
	
	final long min = unit.toMinutes(amt); 
    amt -= subtractAmt(min, unit, TimeUnit.MINUTES);
    
    final long sec = unit.toSeconds(amt); 
    amt -= subtractAmt(sec, unit, TimeUnit.SECONDS);
    
    final long ms = unit.toMillis(amt);
    amt = 0;
    
    return new TimeSlots(hr, min, sec, ms, amt);
}
 
Example 8
Source File: VideoListAdapter.java    From android-player-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the given duration into a time span string.
 *
 * @param duration elapsed time as number of milliseconds.
 * @return the formatted time span string.
 */
@NonNull
private static String millisecondsToString(long duration) {
    final TimeUnit scale = TimeUnit.MILLISECONDS;

    StringBuilder builder = new StringBuilder();
    long days = scale.toDays(duration);
    duration -= TimeUnit.DAYS.toMillis(days);
    if (days > 0) {
        builder.append(days);
        builder.append(days > 1 ? " days " : " day ");
    }

    long hours = scale.toHours(duration);
    duration -= TimeUnit.HOURS.toMillis(hours);
    if (hours > 0) {
        builder.append(String.format(Locale.getDefault(),"%02d:", hours));
    }

    long minutes = scale.toMinutes(duration);
    duration -= TimeUnit.MINUTES.toMillis(minutes);

    long seconds = scale.toSeconds(duration);
    builder.append(String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds));

    return builder.toString();
}
 
Example 9
Source File: VideoListAdapter.java    From android-player-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the given duration into a time span string.
 *
 * @param duration elapsed time as number of milliseconds.
 * @return the formatted time span string.
 */
@NonNull
private static String millisecondsToString(long duration) {
    final TimeUnit scale = TimeUnit.MILLISECONDS;

    StringBuilder builder = new StringBuilder();
    long days = scale.toDays(duration);
    duration -= TimeUnit.DAYS.toMillis(days);
    if (days > 0) {
        builder.append(days);
        builder.append(days > 1 ? " days " : " day ");
    }

    long hours = scale.toHours(duration);
    duration -= TimeUnit.HOURS.toMillis(hours);
    if (hours > 0) {
        builder.append(String.format(Locale.getDefault(),"%02d:", hours));
    }

    long minutes = scale.toMinutes(duration);
    duration -= TimeUnit.MINUTES.toMillis(minutes);

    long seconds = scale.toSeconds(duration);
    builder.append(String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds));

    return builder.toString();
}
 
Example 10
Source File: PippoSettings.java    From pippo with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the duration setting and converts it to minutes.
 * <p/>
 * The setting must be use one of the following conventions:
 * <ul>
 * <li>n MILLISECONDS
 * <li>n SECONDS
 * <li>n MINUTES
 * <li>n HOURS
 * <li>n DAYS
 * </ul>
 *
 * @param name
 * @param defaultValue in minutes
 * @return minutes
 */
public long getDurationInMinutes(String name, long defaultValue) {
    TimeUnit timeUnit = extractTimeUnit(name, defaultValue + " MINUTES");
    long duration = getLong(name, defaultValue);

    return timeUnit.toMinutes(duration);
}