Java Millisecond Current Get getCurrentMillisecondZeroFillString()

Here you can find the source of getCurrentMillisecondZeroFillString()

Description

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

License

Apache License

Return

A string of timestamp.

Declaration

public static String getCurrentMillisecondZeroFillString() 

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();

    /**//from  www . j  av  a 2  s.c  om
     * Returns a string of timestamp with millisecond precision and the length
     * of {@code LONG_TYPE_MAX_LENGTH}.
     * 
     * @return A string of timestamp.
     */
    public static String getCurrentMillisecondZeroFillString() {
        return getCurrentMillisecondZeroFillString(LONG_TYPE_MAX_LENGTH);
    }

    /**
     * Returns a string of timestamp with millisecond 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 getCurrentMillisecondZeroFillString(int length) {
        return getLeftZeroFillString(System.currentTimeMillis(), 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. currentTimeMillis()
  2. currentTimeMillisUTC()
  3. getCurrentMillis()
  4. getCurrentMillisecond()
  5. getCurrentMillisecondString()
  6. getCurrentTimeMillisStr()
  7. getMillins(String yyyyMMddHHmmss)
  8. getMillisDurationString(long millis)
  9. getMillisec_old(String isoDate)