0

I have a function to show an alert if time is between two values.

Eg:

start = 07:00:00

end = 17:00:00

assert start != null;
                        int from = Integer.valueOf(start.replace(":", ""));
                        assert end != null;
                        int to = Integer.valueOf(end.replace(":", ""));
                        Date date = new Date();
                        Calendar c = Calendar.getInstance();
                        c.setTime(date);
                        int t = c.get(Calendar.HOUR_OF_DAY) * 100 + c.get(Calendar.MINUTE);
                        boolean isBetween = to > from && t >= from && t <= to || to < from && (t >= from || t <= to);
                        a = isBetween ? 1 : 0;

                        if(a == 1) { 
                           alert_f();
                           // a is never 1...
                        }

The problem is the a is never 1, even when the actual time is between the start and the end one.

any ideas why?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
RGS
  • 4,062
  • 4
  • 31
  • 67
  • 1
    If your time strings also include the seconds shouldn't you also include the seconds from the calendar when calculating t? – user May 19 '19 at 14:06
  • @Luksprog thanks for your answer! How can I add seconds in my function? or remove from the start/end variables? can you help me? – RGS May 19 '19 at 14:08

3 Answers3

2

java.time and ThreeTenABP

    String start = "07:00:00";
    String end = "17:00:00";
    LocalTime startTime = LocalTime.parse(start);
    LocalTime endTime = LocalTime.parse(end);

    LocalTime now = LocalTime.now(ZoneId.of("Europe/Rome"));

    boolean isBetween = ! now.isBefore(startTime) && now.isBefore(endTime);
    int a = isBetween ? 1 : 0;
    System.out.println("Is between? " + isBetween + "; a = " + a);

When I ran it just now (17:15 in Rome) I got 0 like you, which is expected. If I specify a time zone where it’s not yet 5 PM I get:

Is between? true; a = 1

So you need to pick the correct time zone for the code. If you trust the device time zone, you may use ZoneId.systemDefault().

In my Boolean expression I am using “not before” to mean “equal to or after”.

I am exploiting the fact that your time strings are in ISO 8601 format, the format that LocalTime and other java.time classes parse (and also print) as their default. So we need no explicit formatter for parsing.

The date-time classes you were trying to use, Date and Calendar, are long outdated and poorly designed. Also integer values 70 000 and 170 000 don’t really make sense as representations of your start and end times. Instead I am using LocalTime from java.time, the modern Java date and time API. It’s a time of day without date and without UTC offset, so seems to be exactly what you need here.

Question: Can I use java.time on Android?

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
1

Well, you can make use of SimpleDateFormat and get the time, then simplify it to:

assert start != null;
int from = Integer.valueOf(start.replace(":", ""));
assert end != null;
int to = Integer.valueOf(end.replace(":", ""));
// here the change
int currentTime = Integer.valueOf(new SimpleDateFormat("HH:mm:ss").format(new Date()).replace(":", ""));
boolean isBetween = to > from && currentTime >= from && currentTime <= to || to < from && (currentTime >= from || currentTime <= to);
// end
int a = isBetween ? 1 : 0;
william xyz
  • 710
  • 5
  • 19
1

you can try this way

    try {
        SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date from = parser.parse("2019-12-31 07:00");
        Date to = parser.parse("2019-12-21 17:00");
        int a;
        Date userDate = parser.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
        if (userDate.after(from) && userDate.before(to)) 
            a = 1;
         else
            a = 0;
    } catch (ParseException e) {
        // Invalid date was entered
    }
Mohamed Mo'nes
  • 420
  • 3
  • 14
  • 1
    It's not guaranteed that the times will be in the same day, for example, if the start time is 17:00 and end time is 7:00, comparing them considering it's the same day will not work. I presumed this checking his between condition checking if from is greater than to. – william xyz May 19 '19 at 14:51