Java Utililty Methods Long Number to Time

List of utility methods to do Long Number to Time

Description

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

Method

intconvertTimeToInt(long seconds)
Converts a long seconds value to an int seconds value and takes into account overflow from the downcast by switching to Integer.MAX_VALUE.
if (seconds > Integer.MAX_VALUE) {
    return Integer.MAX_VALUE;
} else {
    return (int) seconds;
StringconvertTimeToString(final long time)
Converting the time into String format.
StringBuilder sb = new StringBuilder();
sb.append(time / MILLISEC_DECIMAL_DIVIDER);
long millisecs = time % MILLISEC_DECIMAL_DIVIDER;
if (millisecs > 0) {
    sb.append(".");
    if (millisecs < SMALLEST_TWO_DIGIT_DECIMAL) {
        sb.append("00");
    } else if (millisecs < SMALLEST_THREE_DIGIT_DECIMAL) {
...
StringconvertTimeToString(final long timeInMillis)
convert Time To String
final Calendar calendar = Calendar.getInstance(Locale.GERMAN);
calendar.setTimeInMillis(timeInMillis);
final SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT_PATTERN);
format.setTimeZone(TimeZone.getTimeZone("GMT+0")); 
return format.format(calendar.getTime());
longconvertTimeUnit(long epochTime, String timeUnit)
convert Time Unit
long modTime = epochTime;
if (timeUnit.equals("hour")) {
    modTime /= MILISEC_PER_HOUR;
} else if (timeUnit.equals("day")) {
    modTime /= MILISEC_PER_DAY;
} else {
    throw new IllegalArgumentException("invalid time unit");
return modTime;
longconvertTimeUnit(long epochTime, String timeUnit)
convert Time Unit
long modTime = epochTime;
if (timeUnit.equals(TIME_UNIT_SEC)) {
    modTime /= MILISEC_PER_SEC;
} else if (timeUnit.equals(TIME_UNIT_MIN)) {
    modTime /= MILISEC_PER_MIN;
} else if (timeUnit.equals(TIME_UNIT_HOUR)) {
    modTime /= MILISEC_PER_HOUR;
} else if (timeUnit.equals(TIME_UNIT_DAY)) {
...
StringconvertUnixTime(long unixtime)
Convert unix time to date,default is yyyy-MM-dd HH:mm:ss.
String formatter = "yyyy-MM-dd HH:mm:ss";
return convertUnixTime(unixtime, formatter);
StringconvertUnixTimeStampToMySql(long timestamp)
convert Unix Time Stamp To My Sql
Date date = new Date(timestamp * 1000L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = "";
try {
    formattedDate = sdf.format(date);
} catch (Exception E) {
    E.printStackTrace();
return formattedDate;
StringcreateCookie(String name, String value, String path, String domain, long stateTimeToLive)
create Cookie
String contextCookie = name + "=" + value;
if (path != null) {
    contextCookie += ";Path=" + path;
if (domain != null) {
    contextCookie += ";Domain=" + domain;
Date expiresDate = new Date(System.currentTimeMillis() + stateTimeToLive);
...
StringcreateFileDateTimeString(final Long ms)
Create a 'file' format date string.
if (ms == null) {
    return NULL;
final SimpleDateFormat sdf = new SimpleDateFormat(FILE_TIME_FORMAT);
sdf.setTimeZone(UTC_TIME_ZONE);
sdf.getCalendar().setTimeZone(UTC_TIME_ZONE);
return createDateTimeString(ms, sdf, FILE_MILLISECOND_SEPERATOR);
StringelapsedTime(long start, long end)
Calculate the elapsed time between two times specified in milliseconds.
if (start > end) {
    return null;
long[] elapsedTime = new long[TIME_FACTOR.length];
for (int i = 0; i < TIME_FACTOR.length; i++) {
    elapsedTime[i] = start > end ? -1 : (end - start) / TIME_FACTOR[i];
    start += TIME_FACTOR[i] * elapsedTime[i];
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumIntegerDigits(2);
StringBuffer buf = new StringBuffer();
for (int i = 0; i < elapsedTime.length; i++) {
    if (i > 0) {
        buf.append(":");
    buf.append(nf.format(elapsedTime[i]));
return buf.toString();