Android Milliseconds Calculate formatDuration(final Resources res, final long millis)

Here you can find the source of formatDuration(final Resources res, final long millis)

Description

Return given duration in a human-friendly format.

License

Apache License

Declaration

public static CharSequence formatDuration(final Resources res,
        final long millis) 

Method Source Code

/*//from   w  w  w  . j av a 2s.  c om
 * Copyright (C) 2009-2014 Johan Nilsson <http://markupartist.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.content.res.Resources;
import com.markupartist.sthlmtraveling.R;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{
    public static final long SECOND_IN_MILLIS = 1000;
    public static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
    public static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
    /**
     * Return given duration in a human-friendly format. For example, "4
     * minutes" or "1 second". Returns only largest meaningful unit of time,
     * from seconds up to hours.
     * <p/>
     * From android.text.format.DateUtils
     */
    public static CharSequence formatDuration(final Resources res,
            final long millis) {
        if (millis >= HOUR_IN_MILLIS) {
            final int hours = (int) ((millis + 1800000) / HOUR_IN_MILLIS);
            return res.getQuantityString(R.plurals.duration_hours, hours,
                    hours);
        } else if (millis >= MINUTE_IN_MILLIS) {
            final int minutes = (int) ((millis + 30000) / MINUTE_IN_MILLIS);
            return res.getQuantityString(R.plurals.duration_minutes,
                    minutes, minutes);
        } else {
            final int seconds = (int) ((millis + 500) / SECOND_IN_MILLIS);
            return res.getQuantityString(R.plurals.duration_seconds,
                    seconds, seconds);
        }
    }
}

Related

  1. millisecondsAfterNearestFifteenMinutes( String date, String time, int milliseconds)
  2. millisecondsAfter(String date, String time, int milliseconds)
  3. getMillisPerUnit(int unit)
  4. getUTCTimeMillis()
  5. getTimeZoneOffsetInMillis()
  6. roundMicrosToMillis(String micros)