1

I have a server configured to time zone IST which is GMT+5:30 and on everyday new JSON file will be generated using Cron job. Generated filename would be 1551139200000.json ie.GMT: Tuesday, February 26, 2019 12:00:00 AM.

How to make this file available to android app only on/after 12:00AM. Currently it could be available to all time-zone which were not hit 12:00 AM. In this case it should access previous day's Json file.

// normal code here.
long fullMsecs = System.currentTimeMillis();
long ist_offset = 330*60*1000;

long filename = (fullMsecs + ist_offset)/86400_000 * 86400_000;
Log.d(TAG, "onCreate:filename" + filename);

// filename would be affixed with required file extension.

PS: this logic is available in famous crossword like puzzle Bonza. In that game new puzzle will be accessed on start-of-the-day ie, 12:00 AM.

kiranking
  • 306
  • 11
  • 29
  • So if the users want the new file early, they can just set their device time zone to Pacific/Kiritimati (which is GMT+14:00)? Would you be able to detect such cheating? – Ole V.V. Feb 26 '19 at 14:18
  • Not necessarily. Just to avoid auto accessing file even though device time NOT yet reached the midnight. – kiranking Feb 26 '19 at 16:39

1 Answers1

2

java.time and ThreeTenABP

    // Today in user’s time zone
    LocalDate today = LocalDate.now(ZoneId.systemDefault());

    // Today’s file name is epoch millis for 12 AM GMT
    long startOfDayGmt = today.atStartOfDay(ZoneOffset.UTC)
            .toInstant()
            .toEpochMilli();
    String fileName = String.valueOf(startOfDayGmt) + ".json";

    System.out.println("File name: " + fileName);

Output when running just now is what you had expected:

File name: 1551139200000.json

Don’t do date and time calculations by hand as in your question. It’s harder than you think, error-prone and hard to read and maintain. java.time, the modern Java date and time API, offers very nice facilities for what you need.

Question: Can I use java.time with Android minSdkVersion 19?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • my app's build config is like `compileSdkVersion 26` `minSdkVersion 19` and `targetSdkVersion 26` . So I couldn't use ZoneId for api 19. But your code is flawless which I tested by changing api level and will use in the future. Thank you! – kiranking Feb 27 '19 at 07:01
  • Thanks, @kiranking. I changed the header to *Question: Can I use java.time with Android minSdkVersion 19?* in an attempt to make it clearer that I already tried to address that in the answer. :-) – Ole V.V. Feb 27 '19 at 07:30