Search code examples
javauniqueidentifierjava.time.instant

Can Instant.toEpochMilli() be used as a unique id?


I have to generate a unique Id which is readable as well hence cannot use UUID. So I thought of using Instant.toEpochMilli(). Can it be used as unique id?


Solution

  • No. I personally faced with situation, when two objects had same id. I have tested System.nanoTime() and it looked OK, but in general case both variants are not correct.

    Correct solutions:

    • UUID.randomUUID()
    • AtomicLong
    • Database sequence