'How to get a unique number everyday from every android device (i.e same number from every device) running on any version of android

I want to get a unique number everyday that can be generated from any android device and every device should generate same number. I tried getting the date but older version of android doesn't support locale time zone, and then I used this code

        val df = DateFormat.getDateInstance()
        var dff = DateFormat.getTimeInstance()
        var localDate = df.format(Date())
        df.timeZone = TimeZone.getTimeZone("gmt")
        val gmtDate = df.format(Date())
        val localTime = dff.format(Date())

But this generated the date in different format from different devices , but I want same format from every version of android above 6.



Solution 1:[1]

tl;dr

java.time.LocalDate
.now( ZoneOffset.UTC )
.atStartOfDay( ZoneOffset.UTC )
.toInstant()
.getEpochSecond()

Details

Your question is unclear. Perhaps what you ask is how to determine the first moment of the day as seen in UTC for the current moment.

Use only java.time classes for date-time work. Never use the terribly flawed legacy classes such as Date, Calendar, and SimpleDateFormat.

The java.time classes are built into Android 26+. For earlier Android, the latest tooling brings most of the functionality via “API de-sugaring”.

Instant startOfTodayUtc = 
    LocalDate
    .now( ZoneOffset.UTC )
    .atStartOfDay( ZoneOffset.UTC )
    .toInstant() ;
long secondsSinceEpochReference = startOfTodayUtc.getEpochSecond() ;

Solution 2:[2]

System.currentTimeMillis() gives you a UTC timestamp, how about that? You can divide it by (1000 * 60 * 60 * 24) to round it to the nearest day (possibly some issues there with leap seconds but you may not care)

Solution 3:[3]

i know your question, you want get a global unique in your every device, i have two methods:

  1. you can build a center generator the unique number;
  2. you can learn Snowflake algorithm? this method use time and ip to generate global unique number;

wish help yuo.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 cactustictacs
Solution 3 shengdoushi