0

I would like to convert LocalDateTime "2021-10-07T21:45:14" to UUID. And then use this UUID.timestamp to use later.

I try to use

UUIDGen.getTimeUUID( LocalDateTime.parse( "2021-10-07T21:45:14" ).getLong( ChronoField.MILLI_OF_DAY ) )

which generates 6a495100-1e88-11b2-b064-0fb9cd38af13, which is not correct date time "1970-01-01 21:45:14.000000.0 UTC"

dname
  • 153
  • 2
  • 10
  • Milli of day is the number of millis that happened in the day so far--note how the time is correct. – Dave Newton Oct 07 '21 at 22:51
  • FYI, UUID was never designed to be a date-time storage type. Some versions of UUID have a date-time embedded, and some do not. – Basil Bourque Oct 07 '21 at 22:51
  • Milli of day always returns a unique number that you can use a unique ID. You will then easily revert it back to the corresponding date. – Harry Coder Oct 08 '21 at 05:03
  • @HarryCoder how can i do it? as you see 6a495100-1e88-11b2-b064-0fb9cd38af13,] is revert back to date time "1970-01-01 21:45:14.000000.0 UTC" which is not correct – dname Oct 08 '21 at 06:26
  • Try this answer : https://stackoverflow.com/questions/15179428/how-do-i-extract-a-date-from-a-uuid-using-java – Harry Coder Oct 08 '21 at 12:12

1 Answers1

0

Option 1: convert LocalDateTime to Instant and then to milliseconds

LocalDateTime datetime = LocalDateTime.parse("2021-10-07T21:45:14");
UUIDGen.getTimeUUID(datetime.toInstant(ZoneOffset.UTC).toEpochMilli());

Option 2: convert Instant to milliseconds

UUIDGen.getTimeUUID(Instant.parse("2021-10-07T21:45:14Z").toEpochMilli()); // Z = UTC
fabiolimace
  • 972
  • 11
  • 13