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

StringformatTimeSec(long time)
Generates string representation of given time specified as long.
StringBuffer str = new StringBuffer();
if (time < 60) {
    str.append(time + " sec");
    return str.toString();
int days = (int) time / 86400;
if (days != 0) {
    str.append(days + " days");
...
StringformatTimespan(int timespan)
format Timespan
String result = "";
int years = timespan / 31536000;
if (years > 0) {
    timespan -= years * 31536000;
    result += String.format(" %dy", years);
int months = timespan / 2592000;
if (months > 0) {
...
StringformatTimeSpanForScheduler(long time)
Format a timespan given in ms as human readable output (using weeks as the maximum time unit and seconds as the smallest)
if (time < 0)
    return "-";
StringBuilder res = new StringBuilder(10);
long s = time / 1000;
long min = 0;
long hr = 0;
long d = 0;
long w = 0;
...
StringformatTimestamp(String timestamp)
format Timestamp
if (timestamp == null || "".equals(timestamp)) {
    return "";
String tempTimeStamp = timestamp + "00000000000000";
StringBuffer stringBuffer = new StringBuffer(tempTimeStamp);
return tempTimeStamp = stringBuffer.substring(0, 13);
StringformatTimestampEnd(String timestamp)
Formats a string timestamp to be used as the end bound filter string in HBase scan.
if (timestamp == null || timestamp == "")
    return timestamp;
timestamp = String.format("%013d", Long.parseLong(timestamp) + 1);
return timestamp;
StringformatTimestampForFilename(final long timestamp)
format Timestamp For Filename
return String.format("%1$tF_%1$tH-%1$tM-%1$tS", timestamp);
StringformatTimestampForLogging(final long rawNanosTimestamp)
format Timestamp For Logging
final long nanosPart = rawNanosTimestamp % 1000;
final long totalMicros = rawNanosTimestamp / 1000;
final long microsPart = totalMicros % 1000;
final long totalMillis = totalMicros / 1000;
final long millisPart = totalMillis % 1000;
final long totalSeconds = totalMillis / 1000;
final long secondsPart = totalSeconds % 60;
final StringBuilder sb = new StringBuilder(3 + 3 + 3 + 3 + 2);
...
StringformatTimestampStart(String timestamp)
Formats timestamp value to be 13 digits long
if (timestamp != null && timestamp != "")
    timestamp = String.format("%013d", Long.parseLong(timestamp));
return timestamp;
StringformatTimeStep(Integer numberOfTimestepsPerYear, int stepNumber)
format Time Step
return String.format("%.3f", (double) stepNumber / numberOfTimestepsPerYear);
StringformatTimeString(long millisecond)
Milliseconds -> hh:mm:ss
millisecond = millisecond < 0 ? -millisecond : millisecond;
int seconds = (int) (millisecond / 1000);
StringBuffer result = new StringBuffer("");
int ss = seconds % 60;
int mm = seconds / 60 % 60;
int hh = seconds / 3600;
if (hh == 0)
    result.append("00");
...