Here you can find the source of secondsToYearsDaysHoursMinutesSeconds(double seconds)
Parameter | Description |
---|---|
seconds | Timestamp |
public static String secondsToYearsDaysHoursMinutesSeconds(double seconds)
//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(); } }