Java Time Format formatTime(long value)

Here you can find the source of formatTime(long value)

Description

Format the given long value as a CQL time literal, using the following time pattern: hh:mm:ss[.fffffffff] .

License

Apache License

Parameter

Parameter Description
value A long value representing the number of nanoseconds since midnight.

Return

The formatted value.

Declaration

public static String formatTime(long value) 

Method Source Code

//package com.java2s;
/*//w w w.j  a  v  a2 s  .c o  m
 *      Copyright (C) 2012-2015 DataStax Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

public class Main {
    /**
     * Format the given long value as a CQL time literal, using the following time pattern: {@code hh:mm:ss[.fffffffff]}.
     *
     * @param value A long value representing the number of nanoseconds since midnight.
     * @return The formatted value.
     * @see <a href="https://cassandra.apache.org/doc/cql3/CQL-2.2.html#usingtime">'Working with time' section of CQL specification</a>
     */
    public static String formatTime(long value) {
        int nano = (int) (value % 1000000000);
        value -= nano;
        value /= 1000000000;
        int seconds = (int) (value % 60);
        value -= seconds;
        value /= 60;
        int minutes = (int) (value % 60);
        value -= minutes;
        value /= 60;
        int hours = (int) (value % 24);
        value -= hours;
        value /= 24;
        assert (value == 0);
        StringBuilder sb = new StringBuilder();
        leftPadZeros(hours, 2, sb);
        sb.append(":");
        leftPadZeros(minutes, 2, sb);
        sb.append(":");
        leftPadZeros(seconds, 2, sb);
        sb.append(".");
        leftPadZeros(nano, 9, sb);
        return sb.toString();
    }

    private static void leftPadZeros(int value, int digits, StringBuilder sb) {
        sb.append(String.format("%0" + digits + "d", value));
    }
}

Related

  1. formatTime(long timeDiffMillis)
  2. formatTime(long timeInMilli)
  3. formatTime(long timeInSecond)
  4. formatTime(long timeInterval, int maxTerms)
  5. formatTime(long timeMillis)
  6. FormatTime(String strTime, char TimeSepartor)
  7. formatTime(String time)
  8. formatTime(String time)
  9. formatTime(String time)