Java Second Format secondstoHM(long sec)

Here you can find the source of secondstoHM(long sec)

Description

Converti une valeur en secondes sous la forme HH:MM

License

Open Source License

Declaration

public static String secondstoHM(long sec) 

Method Source Code

//package com.java2s;
/*/* w  w  w .j  a v a2 s.c  om*/
 * This file is part of Java Tools for hdsdi3g'.
 * 
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * Copyright (C) hdsdi3g for hd3g.tv 2009-2012
 * 
*/

public class Main {
    /**
     * Converti une valeur en secondes sous la forme HH:MM
     */
    public static String secondstoHM(long sec) {
        StringBuffer sb = new StringBuffer();
        int hrs = getHoursInSec(sec);
        if (hrs < 10) {
            sb.append(0);
        }
        sb.append(hrs);
        sb.append(":"); //$NON-NLS-1$
        int min = Math.round(getMinutesInSecWOHours(sec));

        if (min < 10) {
            sb.append(0);
        }

        sb.append(min);
        return sb.toString();
    }

    /**
     * @param sec les secondes a calculer.
     * @return le nombre d'heures que contient sec.
     * @see getMinutesInSecWOHours.
     */
    public static int getHoursInSec(long sec) {
        return (int) Math.floor((float) sec / 3600f); // en heures
    }

    /**
     * @param sec les secondes a calculer.
     * @return le nombre de minutes que contient sec sans les heures.
     * @see getHoursInSec.
     */
    public static float getMinutesInSecWOHours(long sec) {
        float _diff_hours = (float) sec / 3600f; // en heures,minutes
        int diff_hours = (int) Math.floor(_diff_hours); // en heures
        return ((float) _diff_hours - (float) diff_hours) * 60f;
    }
}

Related

  1. printElapsedSeconds(long start)
  2. removeSecondsFromDateString(String sdate)
  3. Second2DateString(int v)
  4. secondsBetween(String from, String to, String format)
  5. secondsToDHMSString(double seconds)
  6. secondsToHumanDate(long seconds)
  7. secondsToString(long seconds)
  8. secondsToStringFormat(long seconds)
  9. secondsToTime(double seconds)