9

In our Java application we are trying to get the UNIX time from the UUID version 1. But it's not giving the correct date time values.

long time = uuid.timestamp();
time = time / 10000L;   // Dividing by 10^4 as it's in 100 nanoseconds precision 
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
c.getTime();

Can someone please help?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Thiyanesh
  • 519
  • 1
  • 6
  • 14
  • 1
    Aren't you dividing by the wrong number (`10^8`)? Timestamp gives the number of 100 ns (10^-7 s) chunks, so you need 10^4 junks to get 1 ms (10^-3 s). Therefore you have to divide by 10000L. – halex Oct 25 '12 at 14:42
  • @Halex fixed the divisor. Thanks – Thiyanesh May 29 '17 at 20:18

5 Answers5

16

From the docs for timestamp():

The resulting timestamp is measured in 100-nanosecond units since midnight, October 15, 1582 UTC.

So you need to offset it from that. For example:

Calendar uuidEpoch = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
uuidEpoch.clear();
uuidEpoch.set(1582, 9, 15, 0, 0, 0); // 9 = October
long epochMillis = uuidEpoch.getTime().getTime();

long time = (uuid.timestamp() / 10000L) + epochMillis;
// Rest of code as before
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    Aren't you dividing `uuid.timestamp()` by the wrong number (10^8)? Timestamp gives the number of 100 ns (10^-7 s) chunks, so you need 10^4 junks to get 1 ms (10^-3 s). Therefore you have to divide by 10000L. OP has the same error in his code. – halex Oct 25 '12 at 14:42
  • 6
    You can't imagine how proud I am of myself for finding a small error in one of your answers :) – halex Oct 25 '12 at 14:49
6

If you using datastax driver, it's:

UUIDs.unixTimestamp(uuid)

http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/utils/UUIDs.html#unixTimestamp(java.util.UUID)

user3324777
  • 113
  • 1
  • 6
0

In my case, the following code worked.

    final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
    UUID uuid = UUID.fromString("6470d760-d93d-11e9-8b32-858313a776ba");
    long  time = (uuid.timestamp() - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH) / 10000;
    // Rest of code as before
0

How to extract time from UUIDs using UuidUtil from uuid-creator:

Instant instant = UuidUtil.getInstant(uuid);

long secs = instant.getEpochSecond(); // seconds since 1970-01-01
long msecs = instant.toEpochMilli(); // millis since 1970-01-01
fabiolimace
  • 972
  • 11
  • 13
0

In MySQL:

SET 
  @t0:=SYSDATE(6)
, @u0:=UUID()
, @t1:=SYSDATE(6)
; SELECT 
  @u0 AS _u0
, @u1:=CONCAT(SUBSTR(@u0,16,3),SUBSTR(@u0,10,4),SUBSTR(@u0,1,8)) AS _u1
, @t0 AS _t0
, FROM_UNIXTIME(CONV(@u1,16,10)/1e7-12219292800) AS _tu
, @t1 AS _t1 \G

Query OK, 0 rows affected (0.00 sec)

*************************** 1. row ***************************
_u0: 060e0537-ce21-11ec-9807-525400432b3b
_u1: 1ecce21060e0537
_t0: 2022-05-07 18:16:17.246698
_tu: 2022-05-07 18:16:17.246750
_t1: 2022-05-07 18:16:17.246755
1 row in set (0.00 sec)
druud62
  • 1
  • 1