0

The closest post I found on my question is How to compare current time with time range? But this doesn't work for me because i need to know if the current time on the users device is between a time range

so i got the current time like this...

DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss a");
    String horaString = dateFormat.format(hour);
    Log.i("ALO","HORAAAAAASSSSTRRRRIIIINNNNGGG---->"+horaString);

    first = horaString.charAt(11);
    Log.i("ALOP","Char at 11 ---->"+first);
    char second=horaString.charAt(12);
    Log.i("ALOP","Char at 12 ---->"+second);
    char third=horaString.charAt(13);
    char fourth=horaString.charAt(14);
    char fith=horaString.charAt(15);


    StringBuilder sb = new StringBuilder();
    sb.append(first);
    sb.append(second);
    sb.append(third);
    sb.append(fourth);
    sb.append(fith);

    String currentTime = sb.toString();

now what is need to do is

6:00>=currentTime<=8:30 //Can i still do the comparison with AM/PM?

So this is what I need to do if currentTime = 8:29 a method that lets me know is True with a boolean.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Felipe Franco
  • 171
  • 3
  • 15

2 Answers2

1

java.time and ThreeTenABP

    final LocalTime rangeBegin = LocalTime.of(6, 0);
    final LocalTime rangeEnd = LocalTime.of(8, 30);

    LocalTime currentTime = LocalTime.now(ZoneId.of("America/Bogota"));

    boolean inClosedRange = ! (currentTime.isBefore(rangeBegin) || currentTime.isAfter(rangeEnd));

If you prefer to trust the device time zone setting rather than a hard-coded (or configured) time zone, use LocalTime.now(ZoneId.systemDefault()).

Since LocalTime hasn’t got an isBeforeOrEqual method nor an isEqualOrAfter, I am putting the condition negatively: if the current time is neither strictly before nor strictly after the range, it must be within it. Some will prefer the longer but also more direct:

    boolean inClosedRange = currentTime.equals(rangeBegin)
            || (currentTime.isAfter(rangeBegin) && currentTime.isBefore(rangeEnd))
            || currentTime.equals(rangeEnd);

You will notice how much simpler it still is than your code using SimpleDateFormat and Date. I personally also find it clearer. I am using and recommending java.time, the modern Java date and time API.

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
0

There are two useful command: After, Before. Here is an example how you can use them:

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");

Date fromTime = dateFormat.parse("6:00");
Date toTime = dateFormat.parse("8:30");
Date currentTime = dateFormat.parse(dateFormat.format(new Date()));

if(currentTime.after(fromTime) && currentTime.before(toTime)) {
    // To do
}
Dmytro Ivanov
  • 1,260
  • 1
  • 8
  • 10