Java Timestamp Convert To timestampToString(long time)

Here you can find the source of timestampToString(long time)

Description

Formats the given time into string like "7w 1d 2h 34m 56s".

License

Open Source License

Parameter

Parameter Description
time A time period in milliseconds to be represented as string

Return

A formatted string

Declaration

private static String timestampToString(long time) 

Method Source Code

//package com.java2s;
/*//w ww  . jav  a  2s .  co  m
 * The MIT License (MIT)
 * <p/>
 * Copyright (c) 2015 Maksym Dominichenko
 * <p/>
 * 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:
 * <p/>
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * <p/>
 * 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 {
    /**
     * Formats the given {@code time} into string like "7w 1d 2h 34m 56s".
     *
     * @param time
     *          A time period in milliseconds to be represented as string
     * @return A formatted string
     */
    private static String timestampToString(long time) {
        int[] t = timestampToParts(time);
        StringBuilder result = new StringBuilder();
        if (t[5] > 0)
            result.append(t[5]).append("w ");
        if (t[4] > 0)
            result.append(t[4]).append("d ");
        if (t[3] > 0)
            result.append(t[3]).append("h ");
        if (t[2] > 0)
            result.append(t[2]).append("m ");
        if (t[1] > 0)
            result.append(t[1]).append("s ");
        if (t[5] == 0 && t[4] == 0 && t[3] == 0 && t[2] == 0 && t[1] < 10 && t[0] > 0)
            result.append(t[0]).append("ms");
        if (result.length() == 0)
            result.append("0ms");
        return result.toString().trim();
    }

    /**
     * Returns the given {@code time} as {@code int} array that has exactly 6 items:
     * <ul>
     * <li>{@code 0} - milliseconds;
     * <li>{@code 1} - seconds;
     * <li>{@code 2} - minutes;
     * <li>{@code 3} - hours;
     * <li>{@code 4} - days;
     * <li>{@code 5} - weeks;
     * </ul>
     * 
     * @param time
     *          A time period in milliseconds to be represented as {@code int} array
     * @return An {@code int} array with time parts
     */
    public static int[] timestampToParts(long time) {
        int s = (int) Math.floor(time / 1000);
        int ms = (int) (time - s * 1000);
        int w = (int) Math.floor(s / 604800);
        s -= w * 604800;
        int d = (int) Math.floor(s / 86400);
        s -= d * 86400;
        int h = (int) Math.floor(s / 3600);
        s -= h * 3600;
        int m = (int) Math.floor(s / 60);
        s -= m * 60;
        return new int[] { ms, s, m, h, d, w };
    }
}

Related

  1. timestampToLong(Timestamp ts)
  2. timestampToMicros(Timestamp timestamp)
  3. timestampToParts(long time)
  4. timestampToPrettyString(long ts)
  5. timestampToSearchString(final Timestamp timestamp)
  6. timestampToTicks(int year, int month, int day, int hours, int minutes, int seconds, int milliseconds)
  7. to_timestamp(String date)
  8. toCalendar(final Timestamp ts)
  9. toCalendar(java.sql.Timestamp stamp)