Java Timestamp Create timestampNonce(final int length)

Here you can find the source of timestampNonce(final int length)

Description

Generates a nonce of the given size by repetitively concatenating system timestamps (i.e.

License

Open Source License

Parameter

Parameter Description
length Positive number of bytes in nonce.

Return

Nonce bytes.

Declaration

public static byte[] timestampNonce(final int length) 

Method Source Code

//package com.java2s;
/* See LICENSE for licensing and NOTICE for copyright. */

public class Main {
    /**/*from   w w  w  . java 2  s.  c  o  m*/
     * Generates a nonce of the given size by repetitively concatenating system
     * timestamps (i.e. {@link System#nanoTime()}) up to the required size.
     *
     * @param  length  Positive number of bytes in nonce.
     *
     * @return  Nonce bytes.
     */
    public static byte[] timestampNonce(final int length) {
        if (length <= 0) {
            throw new IllegalArgumentException(length + " is invalid. Length must be positive.");
        }

        final byte[] nonce = new byte[length];
        int count = 0;
        long timestamp;
        while (count < length) {
            timestamp = System.nanoTime();
            for (int i = 0; i < 8 && count < length; i++) {
                nonce[count++] = (byte) (timestamp & 0xFF);
                timestamp >>= 8;
            }
        }
        return nonce;
    }
}

Related

  1. timeStamp()
  2. timestamp2string()
  3. timestamp4(int unixTime)
  4. timestampDifference(long ts1, long ts2)
  5. timestampMicros()
  6. timestampNow()
  7. timestampStartOfDay(long time)
  8. timestampStringToUnixDate(String s)
  9. toOracleTimestamp(String stringDate, int intFlag)