You can do by minutes itself:
Just convert days to minutes:
Here : 30 days = 43200 minutes = 1 month
PeriodicWorkRequest myWorkRequest =
new PeriodicWorkRequest.Builder(MonthlyWorker.class, 43200, TimeUnit.MINUTES)
.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("my fancy test",ExistingPeriodicWorkPolicy.KEEP, myWorkRequest);
UPDATE:
As per my requirement, I wanted to perform task every 30 minutes in between 8 am to 6 pm
With help of that worker I made for you as you required 1st of every month in between 12 am to 2 am below:
public class TestWorker extends Worker {
private static final String DEFAULT_START_TIME = "00:01"; //12:01 am
private static final String DEFAULT_END_TIME = "03:00"; // 03:00 am
private static final String TAG = "TestWorker";
public TestWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
/**
* Its for time
*/
DateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
/**
* Get Current Calendar Instance
*/
Calendar c = Calendar.getInstance();
// Get current day of month
int day = c.get(Calendar.DAY_OF_MONTH);
/**
* Get current time and compare with our start and end date.
*/
Date date = c.getTime();
String formattedDate = dateFormat.format(date);
try {
Date currentDate = dateFormat.parse(formattedDate);
Date startDate = dateFormat.parse(DEFAULT_START_TIME);
Date endDate = dateFormat.parse(DEFAULT_END_TIME);
/**
* This condition will check that day is 1, and time interval in between give time.
*/
if (day == 1 && currentDate.after(startDate) && currentDate.before(endDate)) {
// Do your work here
}
} catch (ParseException ignored) {
}
return Result.success();
}
}
You can start this test work for X hour interval so it will check every X hour (for example, In your case you can give 2 hours so it will check every two hours)
I do not know whether it will be affected to performance or not, but I think there is no other solution.
You can start worker as below:
PeriodicWorkRequest periodicWork = new PeriodicWorkRequest.Builder(MyWorker.class, 2, TimeUnit.HOURS)
.addTag("MYWORK")
.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("MYWORK", ExistingPeriodicWorkPolicy.REPLACE, periodicWork);
Hope it will help.