Home > Enterprise >  How to get a unique number everyday from every android device (i.e same number from every device) ru
How to get a unique number everyday from every android device (i.e same number from every device) ru

Time:02-17

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.

CodePudding user response:

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)

CodePudding user response:

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() ;
  • Related