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

longdurationInMillis(long startNano)
duration In Millis
final long endNano = System.nanoTime();
return (endNano - startNano) / 1000000;
StringelapsedMillis(long milliseconds)
Returns a string representation of the specified elapsed time in the format "H hours M minutes S seconds".
return elapsedNanos(1000000 * milliseconds);
longelapsedMillis(long startTime)
elapsed Millis
return (long) elapsedTime(startTime, System.nanoTime(), 1e6);
longelapsedMillis(long t0nanos, long t1nanos)
Get the elapsed milliseconds between two nano times
return (t1nanos - t0nanos) / 1000000;
voidensureSleepMillis(long millis)
Ensures the current thread sleeps at least the given milliseconds before returning from this method.
if (millis < 1)
    return;
long now = System.currentTimeMillis();
final long targetTime = now + millis;
do {
    Thread.sleep(millis);
    millis = targetTime - System.currentTimeMillis();
} while (millis > 0);
...
StringexpensiveMethodTakingMillis(final int millis)
expensive Method Taking Millis
try {
    Thread.sleep(millis);
    return "hi";
} catch (InterruptedException e) {
    throw new IllegalStateException(e);
longfiletimeToMillis(long filetime)
Converts a 64-bit NTFS time value (number of 100-nanosecond intervals since January 1, 1601 UTC) to a Java time value (number of milliseconds since January 1, 1970 UTC.)
filetime -= 116444736000000000L;
if (filetime < 0) {
    filetime = -1 - ((-filetime - 1) / 10000);
} else {
    filetime = filetime / 10000;
return filetime;
StringformatDateMillis(long millis)
format Date Millis
String weekdays = "SunMonTueWedThuFriSat";
String months = "JanFebMarAprMayJunJulAugSepOctNovDec";
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(millis));
return weekdays.substring((cal.get(Calendar.DAY_OF_WEEK) - 1) * 3).substring(0, 3) + " "
        + months.substring(cal.get(Calendar.MONTH) * 3).substring(0, 3) + " " + cal.get(Calendar.DATE) + " "
        + cal.get(Calendar.HOUR_OF_DAY) + ":" + (cal.get(Calendar.MINUTE) < 10 ? "0" : "")
        + cal.get(Calendar.MINUTE) + " " + cal.get(Calendar.YEAR);
...
StringFormatDateTime(Calendar p_date, String p_seperator, boolean p_showMilliseconds)
Format Date Time
StringBuilder t_dateTime = new StringBuilder();
t_dateTime.append(FormatDate(p_date, p_seperator) + " ");
t_dateTime.append(FormatTime(p_date.get(Calendar.HOUR_OF_DAY), p_date.get(Calendar.MINUTE),
        p_date.get(Calendar.SECOND), (p_showMilliseconds ? p_date.get(Calendar.MILLISECOND) : -1)));
return t_dateTime.toString();
longfractionOfDayToMilliseconds(float fFractionOfDay)
Convert a duration that is based on fraction-of-a-day to milliseconds.
float fDuration = (fFractionOfDay * 24 * 60 * 60 * 1000);
return (long) fDuration;