Java implementation of Timeflake.
Timeflake is a 128-bit, roughly-ordered, URL-safe UUID. Inspired by Twitter's Snowflake, Instagram's ID and Firebase's PushID.
<dependency>
<groupId>am.ik.timeflake</groupId>
<artifactId>timeflake4j</artifactId>
<version>1.4.0</version>
</dependency>
final Timeflake timeflake = Timeflake.generate();
System.out.println(timeflake.value()); // 1948067690345842174618850429941262698 (java.math.BigInteger)
System.out.println(timeflake.toUuid()); // 01772f27-10f3-091a-ea3f-28e7f6f7296a (java.util.UUID)
System.out.println(timeflake.toInstant()); // 2021-01-23T12:10:25.395Z (java.time.Instant)
System.out.println(timeflake.base62()); // 2lSUuPgi2bGXfZde730O2 (String)
TheadLocalRandom
is used to generate a random number by default. You can change Random
instance as follows:
final Timeflake timeflake = Timeflake.generate(new SecureRandom());
from an UUID
instance
final Timeflake timeflake = Timeflake.valueOf(UUID.fromString("01772f27-10f3-091a-ea3f-28e7f6f7296a"));
System.out.println(timeflake.value()); // 1948067690345842174618850429941262698
from a base62 encoded string
final Timeflake timeflake = Timeflake.valueOf("2lSUuPgi2bGXfZde730O2");
System.out.println(timeflake.value()); // 1948067690345842174618850429941262698
from a big integer value
final Timeflake timeflake = Timeflake.valueOf(new BigInteger("1948067690345842174618850429941262698"));
System.out.println(timeflake.value()); // 1948067690345842174618850429941262698
from a 48-bit timestamp and 80-bit random part
final Instant timestamp = Instant.now();
final BigInteger random = new BigInteger(80, ThreadLocalRandom.current());
final Timeflake timeflake = Timeflake.create(timestamp, random);
See the original warning.
See the original warning.
- Java 8+
Licensed under the MIT License