androidtimestampunique

Timestamp as unique ID


Is there something wrong if i use

Long uniqueId = System.currentTimeMillis()/1000;

String documentName = ""+ uniqueId;

as for example a document name? Unless the user has a wrong Date it will be always unique right? Or is there a better way to create always unique number values? The reason i use Long uniqueId = System.currentTimeMillis()/1000;

is because it has to be unique even if the user generates that document from another device without the same data which means that i cannot create and save unique values and simply add +1 to it.


Solution

  • If you use

    System.currentTimeMillis()/1000

    then if you save 2 files in the same second you will have 2 identical names for 2 different files.

    So i suggest you to use

    UUID.randomUUID().toString()