0

Problem visualization

I have a streak counter that increases if an action in the app is done every day anew. I want to check it upon opening of the app, what is the easiest way?

I know I can just check in a Calendar or Date object, if it's yesterday+1, like here
Check if a date is "tomorrow" or "the day after tomorrow"

But that is not considering the time, right? Because if the action is done on 24.02. 7AM, then it would have to be 25.02. 7AM+ (24hrs) for it to work?

Big_Chair
  • 2,781
  • 3
  • 31
  • 58
  • That is not perfectly clear, sorry. In the case where the action was last done on 24 Feb 07:00, are you saying that you should only increase the counter if it’s done on 25 Feb between 07:00 and 24:00? Or which is the upper limit where it will be reset instead? – Ole V.V. Feb 28 '21 at 07:12
  • Could the user cheat by changing the time zone of the device? – Ole V.V. Feb 28 '21 at 08:33
  • @OleV.V. No that with 7am was just an example. No I want it to be reset if user has not taken action between 00:00 24th and 00:00 25th. It's a counter for a "daily task" that the user has to do 1 time per day, and if he skips one day, the counter resets. – Big_Chair Feb 28 '21 at 11:52
  • @OleV.V. And yeah I thought about it, what if the user cheats? I don't think I can prevent cheating though. – Big_Chair Feb 28 '21 at 11:52

1 Answers1

3

I know I can just check in a Calendar or Date object, if it's yesterday+1 ...

The java.util date-time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.

But that is not considering the time, right? Because if the action is done on 24.02. 7AM, then it would have to be 25.02. 7AM+ (24hrs) for it to work?

The java.time API (the modern date-time API) provides you with LocalDateTime to deal with local date and time (i.e. the date and time of a place and not requiring comparing it with the date and time of another place and hence not dealing with the timezone). However, when it comes to comparing it with the date and time of another place, not in the same timezone, you need ZonedDateTime (to automatically adjust date & time object as per the DST) or OffsetDateTime (to deal with ` fixed timezone offset) etc. Given below is an overview of java.time types:

enter image description here

Demo:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class Main {
    public static void main(String args[]) {
        LocalDate date = LocalDate.of(2020, 2, 23);
        LocalTime time = LocalTime.of(7, 0);
        LocalDateTime ldt = LocalDateTime.of(date, time);
        System.out.println(ldt);

        LocalDateTime afterTenHoursTwentyMinutes = ldt.plusHours(10).plusMinutes(20);
        LocalDateTime tomorrow = ldt.plusDays(1);
        LocalDateTime theDayAfterTomorrow = ldt.plusDays(2);
        System.out.println(afterTenHoursTwentyMinutes);
        System.out.println(tomorrow);
        System.out.println(theDayAfterTomorrow);

        if (!afterTenHoursTwentyMinutes.isAfter(theDayAfterTomorrow)) {
            System.out.println("After 10 hours and 20 minutes, the date & time will not go past " + tomorrow);
        } else {
            System.out.println("After 10 hours and 20 minutes, the date & time will go past " + tomorrow);
        }
    }
}

Output:

2020-02-23T07:00
2020-02-23T17:20
2020-02-24T07:00
2020-02-25T07:00
After 10 hours and 20 minutes, the date & time will not go past 2020-02-24T07:00

Learn more about the modern date-time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Hmm, thank you for the information. But don't you think this is a very complex solution? I just want to check a boolean and see if user did an action in a 24h time frame. – Big_Chair Feb 28 '21 at 11:55
  • 1
    @Big_Chair - Now that you have got to know about the modern date-time API, I suggest you study and practice the **[Trail: Date Time](https://docs.oracle.com/javase/tutorial/datetime/index.html)**. The modern date-time API is quite rich and therefore you should be able to solve any specific problem after some experience with it. Feel free to post questions/comments in case you get stuck at some point. Wish you success! – Arvind Kumar Avinash Feb 28 '21 at 12:22