Java Utililty Methods Time Format

List of utility methods to do Time Format

Description

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

Method

StringformatTime(String time)
format Time
if (time == null || time.trim().length() < 6 || time.trim().equals("000000"))
    return "";
return time.substring(0, 2) + ":" + time.substring(2, 4) + ":" + time.substring(4, 6);
StringformatTime(String time)
format Time
String ret = time;
if (time.length() > 0 && time.indexOf(":") == -1) {
    if (time.length() == 4) {
        ret = time.substring(0, 2) + ":" + time.substring(2);
    } else if (time.length() == 3) {
        ret = "0" + time.substring(0, 1) + ":" + time.substring(1);
return ret;
StringformatTime(String timeStr)
format Time
if (timeStr.length() == 1) {
    timeStr = "0" + timeStr;
return timeStr;
StringformatTime14To12String(String time)
format Time To String
StringBuffer sb = new StringBuffer();
sb.append(time.substring(0, 4) + "-" + time.substring(4, 6));
sb.append("-" + time.substring(6, 8) + " ");
sb.append(time.substring(8, 10) + ":" + time.substring(10, 12));
return sb.toString();
StringformatTime2(long secs)
format Time
StringBuffer rv = new StringBuffer();
int seconds = (int) secs, minutes = 0, hours = 0, days = 0;
days = seconds / (60 * 60 * 24);
hours = seconds / (60 * 60) % 24;
minutes = (seconds / 60) % 60;
seconds = seconds % 60;
if (days > 0) {
    rv.append(days);
...
StringformatTime2(long timeInSeconds)
format Time
String result = "";
long time = timeInSeconds;
long seconds = time % 60;
time /= 60;
long minutes = time % 60;
time /= 60;
long hours = time % 24;
time /= 24;
...
StringformatTime3(long secs)
format Time
int seconds = (int) secs;
int minutes = 0;
int hours = 0;
int days = 0;
days = seconds / (60 * 60 * 24);
hours = seconds / (60 * 60) % 24;
minutes = (seconds / 60) % 60;
seconds %= 60;
...
StringformatTimeAgo(int seconds)
Convert seconds into a human readable format.
if (seconds == 0)
    return "0 seconds"; 
String date = "";
String[] unitNames = { "week", "day", "hour", "minute", "second" };
int[] unitValues = { 604800, 86400, 3600, 60, 1 };
for (int i = 0; i < unitNames.length; i++) {
    int quot = seconds / unitValues[i];
    if (quot > 0) {
...
StringformatTimeArea(Integer hour)
format Time Area
return String.format("%02d", hour);
StringformatTimeDeltaValue(long delta)
format Time Delta Value
if (delta >= 1000)
    return "" + (delta / 1000) + "." + pad(delta % 1000, 3) + " seconds";
if (delta >= 100)
    return "0." + delta + " seconds";
if (delta >= 10)
    return "0.0" + delta + " seconds";
if (delta >= 0)
    return "0.00" + delta + "seconds";
...