Reputation: 16671
I create my login id for a user using the following code
String login = me.prettyprint.cassandra.utils.TimeUUIDUtils.getUniqueTimeUUIDinMillis().toString();
Now i have a requirement to convert this login back to long so I am using the following code
long timeStamp = java.util.UUID.fromString(login).timestamp();
Now i want my login back from the timeStamp. How can i do this.?
Upvotes: 1
Views: 1781
Reputation: 8415
UUID.timestamp()
extracts 60 bits out of total 128 bits that comprise the UUID.
Take a look at https://www.ietf.org/rfc/rfc4122.txt, section 4.1.2: timestamp extracts time_low
, time_mid
and time_hi_and_version
fields. Technically, you can reconstruct the initial UUID if you know the timestamp AND the values of clock_seq_hi_and_reserved
, clock_seq_low
and node
fields. But check the section 4.2.1 of that document - you can try to guess the node
value by assuming it is related to some MAC address, but you have no information about proper values of the clock sequence.
In the end, it is not feasible to reconstruct the UUID from timestamp. If you need the whole 128-bit UUID you will have to pass it around in the full form.
Upvotes: 0
Reputation: 16671
final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
UUID u1 = TimeUUIDUtils.getUniqueTimeUUIDinMillis();
final long t1 = u1.timestamp();
long tmp = (t1 - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH) / 10000;
UUID u2 = TimeUUIDUtils.getTimeUUID(tmp);
long t2 = u2.timestamp();
System.out.println(u2.equals(u1));
System.out.println(t2 == t1);
This works!!
Upvotes: 1