https://github.com/making/timeflake4j
Java implementation of Timeflake
https://github.com/making/timeflake4j
Last synced: 8 months ago
JSON representation
Java implementation of Timeflake
- Host: GitHub
- URL: https://github.com/making/timeflake4j
- Owner: making
- License: mit
- Created: 2021-01-23T11:02:13.000Z (over 5 years ago)
- Default Branch: develop
- Last Pushed: 2023-03-15T00:57:11.000Z (about 3 years ago)
- Last Synced: 2025-07-28T23:08:58.666Z (9 months ago)
- Language: Java
- Homepage:
- Size: 85.9 KB
- Stars: 40
- Watchers: 3
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Timeflake4j
[](https://opensource.org/licenses/MIT) [](https://maven-badges.herokuapp.com/maven-central/am.ik.timeflake/timeflake4j) [](https://www.javadoc.io/doc/am.ik.timeflake/timeflake4j) [](https://github.com/making/timeflake4j/actions)
Java implementation of [Timeflake](https://github.com/anthonynsimon/timeflake).
> Timeflake is a 128-bit, roughly-ordered, URL-safe UUID. Inspired by Twitter's Snowflake, Instagram's ID and Firebase's PushID.
## How to use
```xml
am.ik.timeflake
timeflake4j
1.4.0
```
## Example
### Generate a snowflake
```java
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:
```java
final Timeflake timeflake = Timeflake.generate(new SecureRandom());
```
### Create a snowflake
from an `UUID` instance
```java
final Timeflake timeflake = Timeflake.valueOf(UUID.fromString("01772f27-10f3-091a-ea3f-28e7f6f7296a"));
System.out.println(timeflake.value()); // 1948067690345842174618850429941262698
```
from a base62 encoded string
```java
final Timeflake timeflake = Timeflake.valueOf("2lSUuPgi2bGXfZde730O2");
System.out.println(timeflake.value()); // 1948067690345842174618850429941262698
```
from a big integer value
```java
final Timeflake timeflake = Timeflake.valueOf(new BigInteger("1948067690345842174618850429941262698"));
System.out.println(timeflake.value()); // 1948067690345842174618850429941262698
```
from a 48-bit timestamp and 80-bit random part
```java
final Instant timestamp = Instant.now();
final BigInteger random = new BigInteger(80, ThreadLocalRandom.current());
final Timeflake timeflake = Timeflake.create(timestamp, random);
```
## Note on security
See [the original warning](https://github.com/anthonynsimon/timeflake#note-on-security).
## Note on privacy
See [the original warning](https://github.com/anthonynsimon/timeflake#note-on-privacy).
## Required
* Java 8+
## License
Licensed under the MIT License