Java Time Format formatTime2(long timeInSeconds)

Here you can find the source of formatTime2(long timeInSeconds)

Description

format Time

License

Open Source License

Declaration

public static String formatTime2(long timeInSeconds) 

Method Source Code

//package com.java2s;
/**//from ww  w . j av a 2 s.c  o m
 *    Copyright Masao Nagasaki
 *    Nagasaki Lab
 *    Laboratory of Biomedical Information Analysis,
 *    Department of Integrative Genomics,
 *    Tohoku Medical Megabank Organization, Tohoku University 
 *    @since 2013
 *
 *    This file is part of SUGAR (Subtile-based GUI-Assisted Refiner).
 *    SUGAR is an extension of FastQC (copyright 2010-12 Simon Andrews)
 *
 *    SUGAR 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 3 of the License, or
 *    (at your option) any later version.
 *
 *    SUGAR 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 SUGAR; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

public class Main {
    public static String formatTime2(long timeInSeconds) {
        String result = "";
        long time = timeInSeconds;
        long seconds = time % 60;
        time /= 60;
        long minutes = time % 60;
        time /= 60;
        long hours = time % 24;
        time /= 24;
        long days = time;
        if (days > 0) {
            result += formatTimePart2(days, "day") + " ";
        }
        if (hours > 0) {
            result += formatTimePart2(hours, "hour") + " ";
        }
        if (minutes > 0) {
            result += formatTimePart2(minutes, "minute") + " ";
        }
        if (seconds > 0) {
            result += formatTimePart2(seconds, "second");
        }
        return result;
    }

    private static String formatTimePart2(long number, String unit) {
        String result = number + " " + unit;
        if (number > 1) {
            result += "s";
        }
        return result;
    }
}

Related

  1. formatTime(String time)
  2. formatTime(String time)
  3. formatTime(String timeStr)
  4. formatTime14To12String(String time)
  5. formatTime2(long secs)
  6. formatTime3(long secs)
  7. formatTimeAgo(int seconds)
  8. formatTimeArea(Integer hour)
  9. formatTimeDeltaValue(long delta)