0

I know this may be a beginner question, but here it goes. Right now, I am creating a student management app where student's info is stored in a DB (MySql) from an android application.

Every student, when registering, enters in a start date and end date. The minimum number of days between start and end date is 30 days.

Every 30 days a monthly survey is sent to the students based on the entered dates. If 7/30/2020 is entered as a start date, they will receive their firstly monthly survey on 8/29/2020 and subsequent monthly surveys every 30 days until end date (maximum 18 months).

Does anyone know how to go about setting these parameters or can point me in the right direction (perhaps to another stackpverflow thread)? I have a working registration system, but I'm not aware of how to capture time, use timestamps, or create a timeline for the monthly survey system.

I look forward to hearing from you!

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • I'm sorry, but I do not understand what your question is about. What do you mean by setting these parameters? What parameters? If you need to regularly perform a task, then you better have some sort of batch job that you run on a daily basis . – Shadow Jul 06 '20 at 18:43
  • You will have a better experience here if you read through [How To Ask](https://stackoverflow.com/help/how-to-ask), then write your questions with the details needed to create [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). See, also, [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) by [Jon Skeet](https://stackoverflow.com/users/22656/jon-skeet), who knows a thing or two about how Stack Overflow works best. – Eric Brandt Jul 06 '20 at 18:47
  • Sorry! I didn't necessarily have the question formulated in my head properly but Ole V.V pointed me in the right direction! – Connor Daly Jul 06 '20 at 18:58

1 Answers1

2

java.time and ThreeTenABP

Use LocalDate from java.time, the modern Java date and time API. For a simple demonstration, first declare:

static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("M/d/u");
static final int daysBetweenSurveys = 30;

Then we may do for example something like the following:

    LocalDate start = LocalDate.parse("7/30/2020", dateFormatter);
    LocalDate end = LocalDate.parse("12/5/2020", dateFormatter);
    
    LocalDate surveyDate = start.plusDays(daysBetweenSurveys);
    while (! surveyDate.isAfter(end)) {
        System.out.println("Survey " + surveyDate.format(dateFormatter));
        surveyDate = surveyDate.plusDays(daysBetweenSurveys);
    }

Output:

Survey 8/29/2020
Survey 9/28/2020
Survey 10/28/2020
Survey 11/27/2020

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both 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 non-Android 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
  • Thank you very much! This has pointed me in the right direction! – Connor Daly Jul 06 '20 at 18:56
  • 1
    FYI, the *ThreeTenABP* back-port may not be needed any longer for early Android. See [this article](https://developer.android.com/studio/write/java8-support-table) about *API desugaring* with *Android Gradle Plugin 4.0.0*. – Basil Bourque Jul 06 '20 at 20:00