Java Time Format formatTime(String time)

Here you can find the source of formatTime(String time)

Description

format Time

License

Open Source License

Declaration

public static String formatTime(String time) 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

public class Main {
    private final static String SPACE = " ";
    private final static String MILLISECOND = "ms";
    private final static String SECOND = "s";
    private final static String MINUTE = "m";

    public static String formatTime(String time) {
        Long time_ms = 0L;/*from  ww w  . j a  va2  s.  c o  m*/
        try {
            time_ms = Long.parseLong(time);
        } catch (NumberFormatException nfe) {
            // nfe.printStackTrace();
            return ""; //$NON-NLS-1$
        }

        if (time_ms < 1000) {
            return time_ms + MILLISECOND;
        } else {
            Long time_s = time_ms / 1000;
            Long time_s_ms = time_ms % 1000;

            if (time_s < 60) {
                StringBuilder sb = new StringBuilder();
                sb.append(time_s + SECOND);
                if (time_s_ms != 0) {
                    sb.append(SPACE + time_s_ms + MILLISECOND);
                }
                return sb.toString();
            } else {
                Long time_m = time_s / 60;
                Long time_m_s = time_s % 60;
                StringBuilder sb = new StringBuilder();
                sb.append(time_m + MINUTE);
                if (time_m_s != 0) {
                    sb.append(SPACE + time_m_s + SECOND);
                }
                if (time_s_ms != 0) {
                    sb.append(SPACE + time_s_ms + MILLISECOND);
                }

                return sb.toString();
            }
        }
    }
}

Related

  1. formatTime(long timeInSecond)
  2. formatTime(long timeInterval, int maxTerms)
  3. formatTime(long timeMillis)
  4. formatTime(long value)
  5. FormatTime(String strTime, char TimeSepartor)
  6. formatTime(String time)
  7. formatTime(String time)
  8. formatTime(String time)
  9. formatTime(String timeStr)