Android Long to Date Convert toTimeString(long milliseconds)

Here you can find the source of toTimeString(long milliseconds)

Description

to Time String

License

Apache License

Declaration

public static String toTimeString(long milliseconds) 

Method Source Code

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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static String toTimeString(Date value) {
        if (value != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

            return dateFormat.format(value);
        } else {/* ww w  .ja  v a2 s  .c  om*/
            return "";
        }
    }

    public static String toTimeString(long milliseconds) {
        long secs = milliseconds / 1000;
        long mins = secs / 60;
        long hours = mins / 60;

        secs = secs % 60;
        mins = mins % 60;

        if (hours > 0) {
            return String.format("%d:%02d:%02d", hours, mins, secs);
        }

        return String.format("%d:%02d", mins, secs);
    }
}

Related

  1. toFullDate(long timestamp)
  2. toIso8601(long date, boolean utc)
  3. toJulianDay(long epochMillis)
  4. toJulianDayNumber(long epochMillis)
  5. toString(Long datetime)
  6. stringForTime(long timeMs)
  7. getTimeString(long millis)
  8. parseTime(long time, String pattern)
  9. getDateByInt(long va)