Java Utililty Methods Milliseconds

List of utility methods to do Milliseconds

Description

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

Method

StringreadableTransferRate(long size, long millis)
readable Transfer Rate
double rate = size / ((double) millis);
long rateInt = Math.round(rate) * 1000l;
return readableFileSize(rateInt);
StringrelTimestrMillis(long time)
rel Timestr Millis
long h = time / 3600000;
long m = ((time / 1000) % 3600) / 60;
long s = (((time / 1000) % 3600) % 60);
long ss = time % 1000;
return leadZero(h, 2) + "h:" + leadZero(m, 2) + "m:" + leadZero(s, 2) + "," + leadZero(ss, 3) + "s";
DateresetHourMinSecMill(final Date date)
Reset hours, minutes, seconds and milliseconds
final int hourOfDay = 17;
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
...
longresetMilliseconds(long msec)
reset Milliseconds
int offset = (int) (msec % 1000);
if (offset < 0) {
    offset += 1000;
return msec - offset;
longreverseTimeMillis(long currentTimeMillis)
reverse Time Millis
return Long.MAX_VALUE - currentTimeMillis;
DaterollMockClockMillis(long millis)
Advances (or rewinds) the mock clock by the given number of milliseconds.
if (mockTime == null)
    throw new IllegalStateException("You need to use setMockClock() first.");
mockTime = new Date(mockTime.getTime() + millis);
return mockTime;
introundMicrosToMillis(String micros)
round Micros To Millis
assert micros != null && micros.length() == 4;
double dblMicros = Integer.valueOf(micros).doubleValue();
return (int) Math.round(dblMicros / 10.0);
longroundMillisWithSecondPrecision(long l)
round Millis With Second Precision
return Math.round(((double) l) / 1000) * 1000;
longroundTimeToTheNearestSecondInMilliseconds(long timeInMilliseconds)
Round time in Milliseconds to the nearest second, and return as time in milliseconds
double timeInSeconds = ((double) timeInMilliseconds) / 1000.0;
timeInSeconds = Math.round(timeInSeconds);
long roundedTimeInMilliseconds = (long) (timeInSeconds * 1000.0);
return roundedTimeInMilliseconds;
voidsafeSleepMillis(long milliseconds)
Sleep for specified number of milliseconds without having to bother catching the InterruptedException potentially thrown by the regular sleep method.
try {
    Thread.sleep(milliseconds);
} catch (InterruptedException e) {
    e.printStackTrace();