Java TimeUnit Usage secondsToYearsDaysHoursMinutesSeconds(double seconds)

Here you can find the source of secondsToYearsDaysHoursMinutesSeconds(double seconds)

Description

Converts a timestamp in seconds into its equivalent representation in days, hours, minutes and seconds.

License

Open Source License

Parameter

Parameter Description
seconds Timestamp

Return

String representation in days, hours, minutes and seconds

Declaration

public static String secondsToYearsDaysHoursMinutesSeconds(double seconds) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:/*from  w ww . j  a v  a2 s .  c  o m*/
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Converts a timestamp in seconds into its equivalent representation in days,
     * hours, minutes and seconds.
     *
     * @param seconds Timestamp
     * @return String representation in days, hours, minutes and seconds
     * 
     */
    public static String secondsToYearsDaysHoursMinutesSeconds(double seconds) {
        if (seconds == Double.MAX_VALUE)
            return "infinity";

        long aux_seconds = (long) seconds;
        long days = TimeUnit.SECONDS.toDays(aux_seconds);
        long months = (long) Math.floor((double) days / 30);
        long years = (long) Math.floor((double) months / 12);
        months %= 12;
        days %= 30;
        long hours = TimeUnit.SECONDS.toHours(aux_seconds) % 24;
        long minutes = TimeUnit.SECONDS.toMinutes(aux_seconds) % 60;
        seconds = seconds % 60;
        StringBuilder out = new StringBuilder();
        if (years > 0) {
            if (years == 1)
                out.append(String.format("%d year", years));
            else
                out.append(String.format("%d years", years));
        }

        if (months > 0) {
            if (out.length() > 0)
                out.append(", ");
            if (months == 1)
                out.append(String.format("%d month", months));
            else
                out.append(String.format("%d months", months));
        }

        if (days > 0) {
            if (out.length() > 0)
                out.append(", ");
            if (days == 1)
                out.append(String.format("%d day", days));
            else
                out.append(String.format("%d days", days));
        }

        if (hours > 0) {
            if (out.length() > 0)
                out.append(", ");
            out.append(String.format("%d h", hours));
        }

        if (minutes > 0) {
            if (out.length() > 0)
                out.append(", ");
            out.append(String.format("%d m", minutes));
        }

        if (seconds > 0) {
            if (out.length() > 0)
                out.append(", ");
            out.append(String.format("%.3f s", seconds));
        }

        if (out.length() == 0)
            out.append("0 s");
        return out.toString();
    }
}

Related

  1. secondsFromNow(long seconds)
  2. secondsPassed(int startTime)
  3. secondsToETA(long secondsTotal)
  4. secondsToHMS(long seconds)
  5. secondsToMillis(int seconds)
  6. selectDurationUnitForDisplay(long durationInNanos)
  7. sleep()
  8. sleep(int millis)
  9. sleep(long durationMillis)