Here you can find the source of formatDurationSeconds(long seconds)
public static String formatDurationSeconds(long seconds)
//package com.java2s; /******************************************************************************* * Copyright (c) OSMCB developers//from w ww .j a v a 2s.c o m * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ import java.util.concurrent.TimeUnit; public class Main { public static final long SECONDS_PER_HOUR = TimeUnit.HOURS.toSeconds(1); public static final long SECONDS_PER_DAY = TimeUnit.DAYS.toSeconds(1); public static String formatDurationSeconds(long seconds) { long x = seconds; long days = x / SECONDS_PER_DAY; x %= SECONDS_PER_DAY; int years = (int) (days / 365); days -= (years * 365); int months = (int) (days * 12d / 365d); String m = (months == 1) ? "month" : "months"; if (years > 5) return String.format("%d years", years); if (years > 0) { String y = (years == 1) ? "year" : "years"; return String.format("%d %s %d %s", years, y, months, m); } String d = (days == 1) ? "day" : "days"; if (months > 0) { days -= months * (365d / 12d); return String.format("%d %s %d %s", months, m, days, d); } long hours = TimeUnit.SECONDS.toHours(x); String h = (hours == 1) ? "hour" : "hours"; x -= hours * SECONDS_PER_HOUR; if (days > 0) return String.format("%d %s %d %s", days, d, hours, h); long minutes = TimeUnit.SECONDS.toMinutes(x); String min = (minutes == 1) ? "minute" : "minutes"; if (hours > 0) return String.format("%d %s %d %s", hours, h, minutes, min); else return String.format("%d %s", minutes, min); } }