Java Timestamp Convert To timestampToParts(long time)

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

Description

Returns the given time as int array that has exactly 6 items:
  • 0 - milliseconds;
  • 1 - seconds;
  • 2 - minutes;
  • 3 - hours;
  • 4 - days;
  • 5 - weeks;

License

Open Source License

Parameter

Parameter Description
time A time period in milliseconds to be represented as int array

Return

An int array with time parts

Declaration

public static int[] timestampToParts(long time) 

Method Source Code

//package com.java2s;
/*//from ww  w .j a  va 2  s .  c  om
 * 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 {
    /**
     * 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. timestampToDateString(Timestamp ts)
  2. timestampToDayNumber(long timestamp)
  3. timestampToInternal(java.sql.Timestamp ts)
  4. timestampToLong(Timestamp ts)
  5. timestampToMicros(Timestamp timestamp)
  6. timestampToPrettyString(long ts)
  7. timestampToSearchString(final Timestamp timestamp)
  8. timestampToString(long time)
  9. timestampToTicks(int year, int month, int day, int hours, int minutes, int seconds, int milliseconds)