Java Time Format formatTimeInterval(long in)

Here you can find the source of formatTimeInterval(long in)

Description

return a string a time interval in millisec

License

Apache License

Parameter

Parameter Description
in positive interval millisec

Declaration

public static String formatTimeInterval(long in) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from w w  w  .ja v  a  2 s  . c  o m
     * return a string a time interval in millisec
     * @param in positive interval millisec
     * @returb non-null string i.e. 3:40
     */
    public static String formatTimeInterval(long in) {
        int del = (int) (in + 500) / 1000; // drop millisec
        int seconds = del % 60;
        del /= 60;
        int minute = del % 60;
        del /= 60;
        int hour = del % 24;
        del /= 24;
        int days = del;
        StringBuffer holder = new StringBuffer();

        if (days != 0) {
            holder.append(Integer.toString(days));
            holder.append(":");
            holder.append(d02(hour));
            holder.append(":");
            holder.append(d02(minute));
            holder.append(":");
            holder.append(d02(seconds));
        } else {
            if (hour != 0) {
                holder.append(Integer.toString(hour));
                holder.append(":");
                holder.append(d02(minute));
                holder.append(":");
                holder.append(d02(seconds));
            } else {
                holder.append(d02(minute));
                holder.append(":");
                holder.append(d02(seconds));
            }
        }
        return (holder.toString());
    }

    /**{ method
     @name toString
     @function convert a char to a string
     @param c the char
     @return the String
     }*/
    public static String toString(char c) {
        StringBuffer s = new StringBuffer();
        s.append(c);
        return (s.toString());
    }

    /**
     * convert a 2 digit int to 00 , 02 or 12
     * in integer between 0 and 99
     * @return non-null string
     */
    protected static String d02(int in) {
        if (in == 0) {
            return ("00");
        } else {
            if (in < 10) {
                return ("0" + in);
            }
            return (Integer.toString(in));
        }
    }
}

Related

  1. formatTimeElapsed(long p_millis)
  2. formatTimeInfo(long time)
  3. formatTimeInMilisec(long time)
  4. formatTimeInNanos(long time)
  5. formatTimeInterval(final long time)
  6. formatTimeLength(long ms)
  7. formatTimeLikeTimer(long time, boolean appendMs)
  8. formatTimeNicely(long millis)
  9. formatTimeOffset(long offset)