Java Second Get getCurrentSecondZeroFillString()

Here you can find the source of getCurrentSecondZeroFillString()

Description

Returns a string of timestamp with second precision and the length of LONG_TYPE_MAX_LENGTH .

License

Apache License

Return

A string of timestamp.

Declaration

public static String getCurrentSecondZeroFillString() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static final int LONG_TYPE_MAX_LENGTH = String.valueOf(Long.MAX_VALUE).length();

    /**// w  w w  .j a  v  a 2s.com
     * Returns a string of timestamp with second precision and the length of
     * {@code LONG_TYPE_MAX_LENGTH}.
     * 
     * @return A string of timestamp.
     */
    public static String getCurrentSecondZeroFillString() {
        return getCurrentSecondZeroFillString(LONG_TYPE_MAX_LENGTH);
    }

    /**
     * Returns a string of timestamp with second precision and the length of
     * {@code length}.
     * 
     * @param length
     *            the length of timestamp to return
     * @return A string of timestamp.
     * @throws IllegalArgumentException
     *             If {@code length} is less than the length of current
     *             timestamp
     */
    public static String getCurrentSecondZeroFillString(int length) {
        return getLeftZeroFillString(System.currentTimeMillis() / 1000, length);
    }

    /**
     * Returns a left zero-filled string of a long.
     * 
     * @param src
     *            a long to left zero-fill
     * @param length
     *            the length of zero-filled string
     * @return A string containing left zero-filled long
     */
    private static String getLeftZeroFillString(long src, int length) throws IllegalArgumentException {
        char[] numberChars = String.valueOf(src).toCharArray();
        int numberLength = numberChars.length;

        if (length < numberLength) {
            throw new IllegalArgumentException(
                    "String length must be equal or greater than current timestamp's length:" + numberLength + ".");
        }

        char[] result = new char[length];
        for (int i = 0; i < length; i++) {
            result[i] = '0';
        }

        int pos = length - numberLength;
        for (char digit : numberChars) {
            result[pos] = digit;
            pos++;
        }

        return String.copyValueOf(result);
    }
}

Related

  1. currentTimeSeconds()
  2. getClockSkewInSeconds()
  3. getCronBySecondScheduler(int second)
  4. getCurrentMiliseconds()
  5. getCurrentSecondString()
  6. getCurrentTimeInSeconds()
  7. getCurrentTimeSeconds()
  8. getDayFromSeconds(long seconds)
  9. getFramesFromSeconds(Double seconds)