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

StringformatTimeString(String time)
Formata e Valida String HH:MM para HH:MM:SS
if (time != null && time.length() == 5) {
    int hora = Integer.parseInt(time.substring(0, 2));
    int min = Integer.parseInt(time.substring(3, 5));
    if (hora > 24)
        time = null;
    else if (min > 60)
        time = null;
    else
...
StringformatTimeTakenNs(long startTimeNs, String message)
Formats time elapsed since the given start time (in nanoseconds).
return message + " took " + (System.nanoTime() - startTimeNs) + " ns.";
StringformatTimeToString(long milisec)
format Time To String
StringBuilder str = new StringBuilder();
long tmp = 0;
if (milisec >= 31557032762.3361d) {
    tmp = (long) (milisec / 31557032762.3361d);
    milisec -= tmp * 31557032762.3361d;
    if (tmp > 1)
        str.append(tmp).append(" years ");
    else
...
StringformatTimeToString(long seconds)
format Time To String
int hours = (int) (seconds / 3600);
int remainder = (int) (seconds % 3600);
int minutes = remainder / 60;
seconds = remainder % 60;
return ((hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":"
        + (seconds < 10 ? "0" : "") + seconds);
StringformatTimeUnits(double nanos)
Format time in reasonable units.
if (nanos < 1000) {
    return String.format("%.0fns", nanos);
double micros = nanos / 1000;
if (micros < 1000) {
    return String.format("%.0fus", micros);
double millis = micros / 1000;
...
StringformatTimeZoneID(String id)
format Time Zone ID
return id.replaceAll("_", " "); 
StringformatToTimeStr(String str)
format str to time str (hh:mm)
String result = "";
if (str != null && str.length() > 0) {
    try {
        result = str.substring(0, 2) + ":" + str.substring(2);
    } catch (Exception e) {
        e.printStackTrace();
        result = str;
return result;
StringformatUptime(long uptime)
format Uptime
StringBuilder buf = new StringBuilder();
if (uptime > DAY) {
    long days = (uptime - uptime % DAY) / DAY;
    buf.append(days);
    buf.append(" Days");
    uptime = uptime % DAY;
if (uptime > HOUR) {
...
StringformatVideoRecordingTime(long t)
format Video Recording Time
int m = (int) t / 60;
int s = (int) t - m * 60;
return String.format("%02d:%02d", m, s);
CalendargetCalendarByDateTime(int dateTime, String dateFormat)
get Calendar By Date Time
Date date = toDate(dateTime, dateFormat);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;