Java Milli Second to Minute daysHoursMinutes(long p_milliseconds)

Here you can find the source of daysHoursMinutes(long p_milliseconds)

Description

Convert the given number of milliseconds into an array of longs, where each long represents the days, hours, minutes respectively.

License

Apache License

Parameter

Parameter Description
p_milliseconds a long value representing the number of milliseconds to convert

Return

an array of longs, where long[0] = days, long[1] = hours, and long[2] = minutes.

Declaration

public static long[] daysHoursMinutes(long p_milliseconds) 

Method Source Code

//package com.java2s;
/**/*from ww w  .  j  a  v  a  2 s  . co m*/
 *  Copyright 2009 Welocalize, Inc. 
 *  
 *  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.
 *  
 */

public class Main {
    private static final long MILLIS_PER_SECOND = 1000;
    private static final long SECONDS_PER_MINUTE = 60;
    private static final long MINUTES_PER_HOUR = 60;
    private static final long HOURS_PER_DAY = 24;

    /**
     * Convert the given number of milliseconds into a string of the
     * form "02d 03h 45m", where the abbreviation characters are
     * obtained from the given resource bundle
     *
     * @param p_milliseconds a long value representing the number of
     * milliseconds to convert
     * @param p_dayAbbrev the day abbreviation to use
     * @param p_hourAbbrev the hour abbreviation to use
     * @param p_minuteAbbrev the minute abbreviation to use
     *
     * @return a string representation of the input value
     */
    public static String daysHoursMinutes(long p_milliseconds, String p_dayAbbrev, String p_hourAbbrev,
            String p_minuteAbbrev) {
        return daysHoursMinutes(p_milliseconds, p_dayAbbrev, p_hourAbbrev, p_minuteAbbrev, HOURS_PER_DAY);
    }

    /**
     * Convert the given number of milliseconds into a string of the
     * form "02d 03h 45m", where the abbreviation characters are
     * obtained from the given resource bundle
     *
     * @param p_milliseconds a long value representing the number of
     * milliseconds to convert
     * @param p_dayAbbrev the day abbreviation to use
     * @param p_hourAbbrev the hour abbreviation to use
     * @param p_minuteAbbrev the minute abbreviation to use
     * @param p_hoursPerBusinessDay - The conversion factor that indicates
     * the number of business hours within a day.  This value is an 
     * attribute of a calendar.
     *
     * @return a string representation of the input value
     */
    public static String daysHoursMinutes(long p_milliseconds, String p_dayAbbrev, String p_hourAbbrev,
            String p_minuteAbbrev, long p_hoursPerBusinessDay) {
        long[] dhm = daysHoursMinutes(p_milliseconds, p_hoursPerBusinessDay);
        StringBuffer sb = new StringBuffer();
        sb.append(pad(dhm[0]));
        sb.append(p_dayAbbrev);
        sb.append(" ");
        sb.append(pad(dhm[1]));
        sb.append(p_hourAbbrev);
        sb.append(" ");
        sb.append(pad(dhm[2]));
        sb.append(p_minuteAbbrev);

        return sb.toString();
    }

    /**
     * Convert the given number of milliseconds into an array of longs,
     * where each long represents the days, hours, minutes respectively.
     *
     * @param p_milliseconds a long value representing the number of milliseconds
     * to convert
     *
     * @return an array of longs, where long[0] = days, long[1] = hours, and
     * long[2] = minutes.
     */
    public static long[] daysHoursMinutes(long p_milliseconds) {
        return daysHoursMinutes(p_milliseconds, HOURS_PER_DAY);
    }

    /**
     * Convert the given number of milliseconds into an array of longs,
     * where each long represents the days, hours, minutes respectively.
     *
     * @param p_milliseconds a long value representing the number of milliseconds
     * to convert
     * @param p_hoursPerBusinessDay - The conversion factor that indicates
     * the number of business hours within a day.  This value is an 
     * attribute of a calendar.
     *
     * @return an array of longs, where long[0] = days, long[1] = hours, and
     * long[2] = minutes.
     */
    public static long[] daysHoursMinutes(long p_milliseconds, long p_hoursPerBusinessDay) {
        long[] dhm = new long[3];
        long seconds = p_milliseconds / MILLIS_PER_SECOND;
        long minutes = seconds / SECONDS_PER_MINUTE;
        long hours = minutes / MINUTES_PER_HOUR;
        minutes -= (hours * MINUTES_PER_HOUR);
        long days = hours / p_hoursPerBusinessDay;
        hours -= (days * p_hoursPerBusinessDay);
        dhm[0] = days;
        dhm[1] = hours;
        dhm[2] = minutes;
        return dhm;
    }

    private static String pad(long p_number) {

        return (p_number < 10 ? "0" : "") + p_number;
    }
}

Related

  1. _toBeginOfMinute(long millis)
  2. asHoursMinutesSeconds(long milliseconds)
  3. daysHoursMinutesToMillis(String expression)
  4. hoursMinutes(long p_milliseconds)
  5. isLessThanMinute(final long timeInMillis)
  6. isSameMinuteOfMillis(final long ms1, final long ms2)