Java Second Convert secondsToYWDHMS(long seconds)

Here you can find the source of secondsToYWDHMS(long seconds)

Description

Converti un nombre de secondes en duree (annees/semaines/jours/heures/min/sec).

License

Open Source License

Return

un resultat tel que "39 ans 29 sem 6j 12:32:11" avec an et semaine facultatifs

Declaration

public static String secondsToYWDHMS(long seconds) 

Method Source Code

//package com.java2s;
/*//from  www.j  ava  2  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 un nombre de secondes en duree (annees/semaines/jours/heures/min/sec).
     * @return un resultat tel que "39 ans 29 sem 6j 12:32:11" avec an et semaine facultatifs
     */
    public static String secondsToYWDHMS(long seconds) {
        if (seconds < 0) {
            return "-1"; //$NON-NLS-1$
        }

        StringBuffer sb = new StringBuffer();
        double _seconds = (double) seconds;

        double oneyear = 31536000d;
        /** 365 x 24 x 60 x 60 ) en secondes */
        double _years = _seconds / oneyear;
        double years = Math.floor(_years);
        if (years > 0) {
            sb.append((int) years);
            if (years > 1) {
                sb.append(" years ");
            } else {
                sb.append(" year ");
            }
        }

        double oneweek = 604800d;
        /** 7 x 24 x 60 x 60 en secondes */
        double _weeks = (_years - years) * oneyear / oneweek;
        double weeks = Math.floor(_weeks);
        if (weeks > 0) {
            sb.append((int) weeks);
            if (weeks > 1) {
                sb.append(" weeks ");
            } else {
                sb.append(" week ");
            }
        }

        double oneday = 86400d;
        /** 24 x 60 x 60 en secondes */
        double _days = (_weeks - weeks) * oneweek / oneday;
        double days = Math.floor(_days);
        if (days > 0) {
            sb.append((int) days);
            if (days > 1) {
                sb.append(" days ");
            } else {
                sb.append(" day ");
            }
        }

        double rest = (_days - days) * oneday;
        /** secondes restantes */
        sb.append(secondstoHMS((long) rest));

        return sb.toString();
    }

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

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

        sb.append(":"); //$NON-NLS-1$
        // int secresult = (int) (Math.round((float)min-Math.floor(min))*60);

        int secresult = (int) Math.round((min - Math.floor(min)) * (float) 60);

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

        sb.append(secresult);
        // sb.append();

        // sb.append(secresult);

        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. secondsToTimecode(long seconds)
  2. secondsToTimeDisplay(Object value)
  3. secondsToTimeString(int numSeconds)
  4. secondsToTimeString(long in_seconds)
  5. secondsToTimeString(long seconds)
  6. SecondToMin(double asec)
  7. timeStringToSeconds(String str)
  8. toCentiSeconds(float t)
  9. toDaySecondDifference(long difference)