Java Utililty Methods Millisecond Convert

List of utility methods to do Millisecond Convert

Description

The list of methods to do Millisecond Convert are organized into topic(s).

Method

LonggetTimeInMillisFromNicsDateString(String dateString)
get Time In Millis From Nics Date String
Long ret = null;
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date;
try {
    date = (Date) formatter.parse(dateString);
    DateTime dt = new DateTime(date);
    ret = dt.getMillis();
...
longgetTimeMillis(String dateStr)
get Time Millis
return convertStrToDate(dateStr, "yyyy-MM-dd HH:mm:ss").getTime();
longgetTimeMillis(String time)
get Time Millis
try {
    DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
    DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
    Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);
    return curDate.getTime();
} catch (ParseException e) {
    e.printStackTrace();
return 0;
StringgetTimestamp(long milliseconds)
get Timestamp
SimpleDateFormat dformat = new SimpleDateFormat("YYYY:MM:dd:HH:mm:ss:SS");
Date d = new Date(milliseconds);
String dstr = dformat.format(d);
return dstr;
StringgetTimestampMillis(Locale locale)
Gets timestamp millis.
return new SimpleDateFormat("yyyyMMddhhmmssSSS", locale).format(new Date());
StringgetTimeStr(final long millis)
get Time Str
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis)
        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis));
long hours = TimeUnit.MILLISECONDS.toHours(millis);
StringBuilder b = new StringBuilder();
b.append(hours == 0 ? "" : String.valueOf(hours) + "h");
b.append(minutes == 0 ? "" : String.valueOf(minutes) + "m");
long seconds = millis - TimeUnit.MINUTES.toMillis(TimeUnit.MILLISECONDS.toMinutes(millis));
b.append(secondFormat.format(seconds / 1000.0) + "s");
...
StringgetTimeString(long millis)
get Time String
Date date = new Date(millis);
return sdf.format(date);
StringgetTimeString(long timelnMillis)
get Time String
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timelnMillis);
Date date = calendar.getTime();
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
String newTypeDate = f.format(date);
return newTypeDate;
StringgetTimeTakenAsSecondsString(long timeTakenMillis)
get Time Taken As Seconds String
String result = "0";
if (timeTakenMillis > 0) {
    float timeSeconds = timeTakenMillis / 1000f;
    result = getSecondsFormatter().format(timeSeconds);
return result;
StringmillisecondConvertToStringTime(final long milliseconds)
Convert the millisecond to String time.
DateFormat formatterTimeGMT = new SimpleDateFormat(TIME_FORMAT);
formatterTimeGMT.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_GMT));
Date date = new Date();
date.setTime(milliseconds);
String timeString = formatterTimeGMT.format(date);
return timeString;