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

The following examples show how to use java.util.concurrent.TimeUnit#toDays() . 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: FileTime.java    From jdk-1.7-annotated with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes a new instance of this class.
 */
DaysAndNanos(long value, TimeUnit unit) {
    long scale;
    switch (unit) {
        case DAYS         : scale = C0; break;
        case HOURS        : scale = C1; break;
        case MINUTES      : scale = C2; break;
        case SECONDS      : scale = C3; break;
        case MILLISECONDS : scale = C4; break;
        case MICROSECONDS : scale = C5; break;
        case NANOSECONDS  : scale = C6; break;
        default : throw new AssertionError("Unit not handled");
    }
    this.days = unit.toDays(value);
    this.excessNanos = unit.toNanos(value - (this.days * scale));
}
 
Example 2
Source File: AbstractManagedChannelImplBuilder.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public final T idleTimeout(long value, TimeUnit unit) {
  checkArgument(value > 0, "idle timeout is %s, but must be positive", value);
  // We convert to the largest unit to avoid overflow
  if (unit.toDays(value) >= IDLE_MODE_MAX_TIMEOUT_DAYS) {
    // This disables idle mode
    this.idleTimeoutMillis = ManagedChannelImpl.IDLE_TIMEOUT_MILLIS_DISABLE;
  } else {
    this.idleTimeoutMillis = Math.max(unit.toMillis(value), IDLE_MODE_MIN_TIMEOUT_MILLIS);
  }
  return thisT();
}
 
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: 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 6
Source File: AbstractManagedChannelImplBuilder.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public final T idleTimeout(long value, TimeUnit unit) {
  checkArgument(value > 0, "idle timeout is %s, but must be positive", value);
  // We convert to the largest unit to avoid overflow
  if (unit.toDays(value) >= IDLE_MODE_MAX_TIMEOUT_DAYS) {
    // This disables idle mode
    this.idleTimeoutMillis = ManagedChannelImpl.IDLE_TIMEOUT_MILLIS_DISABLE;
  } else {
    this.idleTimeoutMillis = Math.max(unit.toMillis(value), IDLE_MODE_MIN_TIMEOUT_MILLIS);
  }
  return thisT();
}
 
Example 7
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 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: PippoSettings.java    From pippo with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the duration setting and converts it to days.
 * <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 days
 * @return days
 */
public long getDurationInDays(String name, long defaultValue) {
    TimeUnit timeUnit = extractTimeUnit(name, defaultValue + " DAYS");
    long duration = getLong(name, defaultValue);

    return timeUnit.toDays(duration);
}