Java 如何生成基于时间的 UUID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18244897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate time based UUIDs?
提问by dogfish
I want to generate time based UUID in java - java.util.UUID.randomUUID() genrates a UUID version 4. How to generate a version 1 (time based) UUID ? Is there a separate library for that or is it some how provided in the Java 7 API and I am missing it.
我想在 java 中生成基于时间的 UUID - java.util.UUID.randomUUID() 生成 UUID 版本 4。如何生成版本 1(基于时间的)UUID?是否有一个单独的库,或者它是在 Java 7 API 中如何提供的,我错过了它。
回答by findsandeep
Be very careful using com.eaio.uuid.UUID, if you need to generate GUID by time other than current time, as this will always generate the same UUID for given time. So if your data has requirement to generate UUID, where records may have same time, this will generate same UUID.
使用 com.eaio.uuid.UUID 时要非常小心,如果您需要按当前时间以外的时间生成 GUID,因为这将始终在给定时间生成相同的 UUID。因此,如果您的数据需要生成 UUID,其中记录可能具有相同的时间,这将生成相同的 UUID。
We needed this to generate UUID for records, which are created ahead and also multiple records at same time, we tried to tweak it by trying to use UUIDGen.createTime(). We found that it has a side effect to mutate the time for records out of order. If a subsequent request to create UUID
has a time stamp older than previous request, you end up having a UUID
, which is based on previous request's time stamp.
我们需要它来为记录生成 UUID,这些记录是提前创建的,同时也是多条记录,我们尝试通过尝试使用 UUIDGen.createTime() 来调整它。我们发现改变记录乱序的时间有副作用。如果后续创建请求的UUID
时间戳早于前一个请求,您最终会得到一个UUID
基于前一个请求的时间戳的 。
After all the analysis, we had to simply go back to create UUID
at the time of creation, so that we don't have to pass it as an argument. Whichever solution we come up with to generate UUID
at a later time, every one has a drawback.
分析完之后,我们只好简单地在创建UUID
的时候回过头来创建,这样我们就不必将它作为参数传递了。无论我们以后想出哪种解决方案来生成UUID
,每个解决方案都有一个缺点。
回答by Avinash Mishra
String timeuuid = com.datastax.driver.core.utils.UUIDs.timeBased().toString();
have a look at https://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/utils/UUIDs.html
看看 https://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/utils/UUIDs.html
回答by Vadzim
FasterXML Java Uuid Generator (JUG)
FasterXML Java Uuid 生成器 (JUG)
https://github.com/cowtowncoder/java-uuid-generator
https://github.com/cowtowncoder/java-uuid-generator
UUID uuid = Generators.timeBasedGenerator().generate();
回答by dkb
Cassandra's implementationor in Githubof type 1 UUID: usageDo note that you do not compare these uuids like uuid1>uuid2 etc because of known bug
Cassandra 的实现或在Github 中的类型 1 UUID:用法请注意,由于已知错误,您不要比较这些 uuid,如 uuid1>uuid2 等
回答by tina
To test our application, we have to generate data which has time-based UUID, using com.eaio.uuid.UUID we generated old time stamp UUIDs and hence were able to control the timestamp as per our test cases. Note: - We cannot generate future timestamp UUID
为了测试我们的应用程序,我们必须生成具有基于时间的 UUID 的数据,使用 com.eaio.uuid.UUID 我们生成旧时间戳 UUID,因此能够根据我们的测试用例控制时间戳。 注意: - 我们无法生成未来时间戳 UUID
We generated in below way -
我们以以下方式生成 -
import com.eaio.uuid.UUID;
import com.eaio.uuid.UUIDGen;
long time = DateUtil.getEpochtimeFromDate("21-06-2018 12:30:31", "dd-M-yyy hh:hh:ss");
UUID time_based_uuid = new UUID(UUIDGen.createTime(time), UUIDGen.getClockSeqAndNode());
回答by Bhavik Prajapati
To generate time based UUIDs in maven project you have add the dependency of Generator that is generate time based UUIDs.
要在 Maven 项目中生成基于时间的 UUID,您需要添加生成器的依赖项,即生成基于时间的 UUID。
<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>3.1.4</version>
</dependency>
If you have a normal java project you have to import library of Generator java-uuid-generator.
如果你有一个普通的 java 项目,你必须导入 Generator java-uuid-generator 的库。
Then generate the UUID:
然后生成 UUID:
UUID uuid= Generators.timeBasedGenerator().generate();
Now if you want to check the UUID time and date check here.
现在,如果您想检查 UUID 时间和日期,请在此处检查。
Enter generated UUID and your current time zone.
输入生成的 UUID 和您当前的时区。