Java Time Format formatTimeToString(long milisec)

Here you can find the source of formatTimeToString(long milisec)

Description

format Time To String

License

Open Source License

Declaration

public static String formatTimeToString(long milisec) 

Method Source Code

//package com.java2s;
/*//from   w  w w.jav a2s  . c o m
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Ziver Koc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    public static String formatTimeToString(long milisec) {
        StringBuilder str = new StringBuilder();
        long tmp = 0;

        // Years
        if (milisec >= 31557032762.3361d) {
            tmp = (long) (milisec / 31557032762.3361d);
            milisec -= tmp * 31557032762.3361d;
            if (tmp > 1)
                str.append(tmp).append(" years ");
            else
                str.append(tmp).append(" year ");
        }
        // Months
        if (milisec >= 2629743830l) {
            tmp = (long) (milisec / 2629743830l);
            milisec -= tmp * 2629743830l;
            if (tmp > 1)
                str.append(tmp).append(" months ");
            else
                str.append(tmp).append(" month ");
        }
        // Days
        if (milisec >= 86400000) {
            tmp = (long) (milisec / 86400000);
            milisec -= tmp * 86400000;
            if (tmp > 1)
                str.append(tmp).append(" days ");
            else
                str.append(tmp).append(" day ");
        }
        // Hours
        if (milisec >= 3600000) {
            tmp = (long) (milisec / 3600000);
            milisec -= tmp * 3600000;
            if (tmp > 1)
                str.append(tmp).append(" hours ");
            else
                str.append(tmp).append(" hour ");
        }
        // Minutes
        if (milisec >= 60000) {
            tmp = (long) (milisec / 60000);
            milisec -= tmp * 60000;
            str.append(tmp).append(" min ");
        }
        // sec
        if (milisec >= 1000) {
            tmp = (long) (milisec / 1000);
            milisec -= tmp * 1000;
            str.append(tmp).append(" sec ");
        }
        if (milisec > 0) {
            str.append(milisec).append(" milisec ");
        }

        return str.toString();
    }
}

Related

  1. formatTimestampStart(String timestamp)
  2. formatTimeStep(Integer numberOfTimestepsPerYear, int stepNumber)
  3. formatTimeString(long millisecond)
  4. formatTimeString(String time)
  5. formatTimeTakenNs(long startTimeNs, String message)
  6. formatTimeToString(long seconds)
  7. formatTimeUnits(double nanos)
  8. formatTimeZoneID(String id)
  9. formatToTimeStr(String str)