0

I'm currently trying to create a firebase JobDispatcher within my android app that will start a job around 3am on a monday, regardless of when the job is created. I have seen examples of using Joda-Time, the Period class and TemporalAdjusters, but I am trying to support API levels 16 and up, so I need something that will work with those.

Currently I am building a new job with the following constraint (among others) .setTrigger(Trigger.executionWindow(secondsUntilMonday, secondsUntilMonday + toleranceInterval))

But I can't seem to find any examples of how to set my secondsUntilMonday to the number of seconds between when the method is called and around the next time 3am on a Monday rolls around.

Please help!

Roaby
  • 3
  • 2
  • `Duration`, `TemporalAdjusters` and the other classes from java.time (the modern Java date and time API; I don’t think you need `Period` here, though) are available for lower-level Android in [the ThreeTenABP library](https://github.com/JakeWharton/ThreeTenABP), the Android backport (ThreeTen for JSR-310). See [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Oct 06 '18 at 08:00

2 Answers2

1

Here is a way that may help you:

@Test
public void computeSecondsToMonday(){
    Calendar c = Calendar.getInstance();
    System.out.print("\r\n" + c.getTime().toString());

    long millisNow = c.getTimeInMillis();

    // let's advance to nex Monday

    int dw = c.get(Calendar.DAY_OF_WEEK);

    // days until monday
    int daysToMonday = 0;
    if(dw >= Calendar.MONDAY){
        daysToMonday = Calendar.SATURDAY - dw + Calendar.MONDAY;
    } else {
        daysToMonday = Calendar.MONDAY - dw;
    }

    // now add days to Monday
    c.add(Calendar.DAY_OF_MONTH, daysToMonday);
    c.set(Calendar.HOUR_OF_DAY, 3);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    // we're in next Monday 3:00 AM
    System.out.print("\r\n" + c.getTime().toString());

    // compute number of millis until Monday
    long millisInMonday = c.getTimeInMillis();
    long millisToMonday = millisInMonday - millisNow;

    // convert millis to sec
    long secondsToMonday = millisToMonday / 1000;

    System.out.print(String.format("\r\nSeconds Until Monday: %d", secondsToMonday));

}

Here's an output from ExampleUnitTest in Android Studio:

Sat Oct 06 09:41:54 WEST 2018
Mon Oct 08 03:00:00 WEST 2018
Seconds Until Monday: 148686

Just adapt the method to your needs, like for example:

public long computeSecondsToMonday(){
    ···
    return secondsToMonday;
}

Hope this is what you need.

alexscmar
  • 421
  • 5
  • 11
0

java.time

    ZoneId here = ZoneId.of("America/Danmarkshavn");
    ZonedDateTime now = ZonedDateTime.now(here);
    ZonedDateTime nextMondayAtThree = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY))
            .with(LocalTime.of(3, 0));
    long secondsUntilMonday = ChronoUnit.SECONDS.between(now, nextMondayAtThree);
    System.out.println(nextMondayAtThree);
    System.out.println("Seconds: " + secondsUntilMonday);

When I ran this snippet just now, the output was:

2018-10-08T03:00Z[America/Danmarkshavn]
Seconds: 147419

Please substitute your time zone since it probably isn’t America/Danmarkshavn.

I also invite you to compare not just the number of lines, but in particular the ease of reading with the answer using the outdated Calendar class (don’t misunderstand me: if you wanted to use the Calendar class, that would be a good answer; I just don’t think you will want to use the Calendar class when you compare).

Question: Can I use java.time on Android API levels 16 and up?

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 new Android devices (from API level 26, I’m told) the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161