Java Unix time unixTimeToString(int time, int precision)

Here you can find the source of unixTimeToString(int time, int precision)

Description

unix Time To String

License

Apache License

Declaration

public static String unixTimeToString(int time, int precision) 

Method Source Code

//package com.java2s;
/*//from   w  ww .  j av a  2  s .c  o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you 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 {
    /** Helper for CAST({timestamp} AS VARCHAR(n)). */
    public static String unixTimeToString(int time) {
        return unixTimeToString(time, 0);
    }

    public static String unixTimeToString(int time, int precision) {
        final StringBuilder buf = new StringBuilder(8);
        unixTimeToString(buf, time, precision);
        return buf.toString();
    }

    private static void unixTimeToString(StringBuilder buf, int time,
            int precision) {
        int h = time / 3600000;
        int time2 = time % 3600000;
        int m = time2 / 60000;
        int time3 = time2 % 60000;
        int s = time3 / 1000;
        int ms = time3 % 1000;
        int2(buf, h);
        buf.append(':');
        int2(buf, m);
        buf.append(':');
        int2(buf, s);
        if (precision > 0) {
            buf.append('.');
            while (precision > 0) {
                buf.append((char) ('0' + (ms / 100)));
                ms = ms % 100;
                ms = ms * 10;
                --precision;
            }
        }
    }

    private static void int2(StringBuilder buf, int i) {
        buf.append((char) ('0' + (i / 10) % 10));
        buf.append((char) ('0' + i % 10));
    }
}

Related

  1. unixtime()
  2. unixTimestamp(int year, int month, int day, int hour, int minute, int second)
  3. unixTimestampToString(long timestamp)
  4. unixtimeToDate(long time)
  5. unixTimeToString(int time)
  6. unixToGST(double unix)
  7. UnixToJulianDays(double unix)