Java Timestamp Format formatElapsed(long elapsed)

Here you can find the source of formatElapsed(long elapsed)

Description

Format Elapsed Time

License

Open Source License

Parameter

Parameter Description
elapsed time in ms

Return

formatted time string 1'23:59:59.999

Declaration

public static String formatElapsed(long elapsed) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
 * Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
 * are Copyright (C) 1999-2005 Jorg Janke.
 * All parts are Copyright (C) 1999-2005 ComPiere, Inc.  All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/

import java.sql.Timestamp;

public class Main {
    /**************************************************************************
     *    Format Elapsed Time//  ww  w.java 2 s. c  o  m
     *    @param start start time or null for now
     *    @param end end time or null for now
     *    @return formatted time string 1'23:59:59.999
     */
    public static String formatElapsed(Timestamp start, Timestamp end) {
        long startTime = 0;
        if (start == null)
            startTime = System.currentTimeMillis();
        else
            startTime = start.getTime();
        //
        long endTime = 0;
        if (end == null)
            endTime = System.currentTimeMillis();
        else
            endTime = end.getTime();
        return formatElapsed(endTime - startTime);
    }

    /**
     *    Format Elapsed Time until now
     *    @param start start time
     *   @return formatted time string 1'23:59:59.999
     */
    public static String formatElapsed(Timestamp start) {
        if (start == null)
            return "NoStartTime";
        long startTime = start.getTime();
        long endTime = System.currentTimeMillis();
        return formatElapsed(endTime - startTime);
    }

    /**
     *    Format Elapsed Time
     *   @param elapsed time in ms
     *   @return formatted time string 1'23:59:59.999
     */
    public static String formatElapsed(long elapsed) {
        long miliSeconds = elapsed % 1000;
        elapsed = elapsed / 1000;
        long seconds = elapsed % 60;
        elapsed = elapsed / 60;
        long minutes = elapsed % 60;
        elapsed = elapsed / 60;
        long hours = elapsed % 24;
        long days = elapsed / 24;
        //
        StringBuffer sb = new StringBuffer();
        if (days != 0)
            sb.append(days).append("'");
        if (hours != 0)
            sb.append(get2digits(hours)).append(":");
        if (minutes != 0)
            sb.append(get2digits(minutes)).append(":");
        sb.append(get2digits(seconds)).append(".").append(miliSeconds);
        return sb.toString();
    }

    /**
     *    Get Minimum of 2 digits
     *   @param no number
     *   @return String
     */
    private static String get2digits(long no) {
        String s = String.valueOf(no);
        if (s.length() > 1)
            return s;
        return "0" + s;
    }
}

Related

  1. formatDateToTimestamp(String string)
  2. formatDateToUnixTimestamp(Date date)
  3. formatDateWithCinderellaTime(Date date)
  4. formatDefaultDate(Timestamp time)
  5. formatDuration(final long duration)
  6. formatFileSize(Number data)
  7. formatGMTTimestampToDate(Timestamp time)
  8. formatJDBCTimeStamp(final java.util.Date date)
  9. formatJDBCTimeStamp(final java.util.Date date)