Java Utililty Methods Second Format

List of utility methods to do Second Format

Description

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

Method

StringformatTime(Date d, Locale locale, boolean withSeconds)
format Time
int style = (withSeconds ? DateFormat.MEDIUM : DateFormat.SHORT);
DateFormat df = DateFormat.getTimeInstance(style, getSafeLocale(locale));
return df.format(d);
StringformatTime(double seconds)
format Time
return df.format(seconds) + " s";
StringformatTime(float seconds)
Formats a time in seconds into HH:MM:SS:MS
int s = (int) seconds;
int ms = (int) ((seconds - s) * 100);
int h = s / 3600;
int m = (s - (h * 3600)) / 60;
s = s % 60;
String hStr = h < 10 ? "0" + h : Integer.toString(h);
String mStr = m < 10 ? "0" + m : Integer.toString(m);
String sStr = s < 10 ? "0" + s : Integer.toString(s);
...
StringformatTimeInSeconds(int v)
Formats the passed value in seconds.
if (v <= 0)
    return "";
int hours = v / 3600;
int remainder = v % 3600;
int minutes = remainder / 60;
int seconds = remainder % 60;
StringBuffer text = new StringBuffer();
if (hours > 0) {
...
voidformatTimeSecondsSinceEpoch(final long time, StringBuilder sbuffer)
Convert timestamp to seconds since epoch with 3 decimal places.
String timeString = Long.toString(time);
int len = timeString.length();
if (len < 3) {
    sbuffer.append(timeString);
    return;
sbuffer.append(timeString.substring(0, len - 3));
sbuffer.append('.');
...
StringformatTimeSpanSeconds(long span)
format Time Span Seconds
final int minute = 60;
final int hour = 60 * 60;
final int day = 24 * hour;
if (span < minute) {
    return span + "s";
} else if (span < hour) {
    return (span / 60) + "m " + (span % 60) + "s";
} else if (span < day) {
...
StringformatTimeWithOutSeconds(java.util.Date d)
format Time With Out Seconds
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
return sdf.format(d);
StringgetDHMS(long totalSecond)
Get a String in HH:MM:SS format for a gievn total seconds (float) total seconds = hour*3600 + minute*60 +seconds, Michael Nguyen
String time = "00:00:00";
if (totalSecond > 0) {
    String dayStr = "";
    long days = totalSecond / (3600 * 24);
    if (days > 0) {
        dayStr = days + " days ";
        totalSecond -= (3600 * 24);
    DecimalFormat decimalFormat = new DecimalFormat("00");
    long hour = totalSecond / 3600;
    if (hour > 0)
        time = decimalFormat.format(hour);
    else
        time = "00";
    time += ":";
    long minute = (totalSecond - (hour * 3600)) / 60;
    if (minute > 0)
        time += decimalFormat.format(minute);
    else
        time += "00";
    time += ":";
    long second = totalSecond - (hour * 3600) - (minute * 60);
    if (second > 0)
        time += decimalFormat.format(second);
    else
        time += "00";
    return dayStr + time;
} else {
    return time;
StringgetElapsedSeconds(long startTime)
get Elapsed Seconds
return DEFAULT_DURATION_FORMAT_SEC.format((System.currentTimeMillis() - startTime) / 1000.0);
StringgetFormatMSecond(long long_)
get Format M Second
String strdate = "0";
if (long_ > 0) {
    strdate = long_ + "";
return strdate;