Java Utililty Methods Millisecond Format

List of utility methods to do Millisecond Format

Description

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

Method

longmillisOf(long timestamp)
millis Of
return timestamp / 10000;
intmillisPerBeat(final float bpm)
Given a beats-per-minute rate, returns the number of milliseconds in a single beat.
return (int) (60000f / bpm);
longmillisSince(long from)
millis Since
long until = System.currentTimeMillis();
return until - from;
StringmillisToDate(long millis, String format)
millis To Date
return new SimpleDateFormat(format).format(new Date(millis));
longmillisTrimTo(long millis, long unitInMillis)
millis Trim To
long remains = millis % unitInMillis;
return millis - remains;
StringparseDate(long millisec, String format)
parse Date
try {
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    Date formattedDate = new Date(millisec);
    String result = formatter.format(formattedDate);
    return result;
} catch (Exception e) {
    e.printStackTrace();
return null;
StringprettyPrintMillis(long millis)
Prints a time for display
return prettyPrintTime(millis, TimeUnit.MILLISECONDS);
StringprettyPrintMillis(long millis)
Prints a time for display
if (millis < 1000)
    return millis + " milliseconds";
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
double toPrint = ((double) millis) / 1000;
if (toPrint < 300) {
    return nf.format(toPrint) + " seconds";
toPrint = toPrint / 60;
if (toPrint < 120) {
    return nf.format(toPrint) + " minutes";
toPrint = toPrint / 60;
return nf.format(toPrint) + " hours";
StringprettyPrintTime(long millis)
Prints a time for display
if (millis < 1000)
    return millis + " milliseconds";
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
double toPrint = ((double) millis) / 1000;
if (toPrint < 300) {
    return nf.format(toPrint) + " seconds";
toPrint = toPrint / 60;
if (toPrint < 120) {
    return nf.format(toPrint) + " minutes";
toPrint = toPrint / 60;
return nf.format(toPrint) + " hours";
StringprintDate(long millis)
print Date
return DATE_FORMAT.format(new Date(millis));