-9

I need to compare the time part from 2 date/time variables to see if the time is inbetween the current time in android. How can I do that?

    Boolean InBetweenTime(Date currentTime, Date StartTime, Date EndTime)
{
    Boolean result = false;
    Calendar calendar = Calendar.getInstance();
    if ((!currentTime.before(StartTime))&&(!currentTime.after(EndTime))){
        //between time
    } else {
        //not inbetween time
    }
    return result;
}
MTplus
  • 1,377
  • 3
  • 21
  • 43
  • 2
    Show what you've already tried – CodeMatrix Oct 22 '18 at 13:07
  • Explain better your problem please. – A. Wolf Oct 22 '18 at 13:08
  • 5
    You asked a [question](https://stackoverflow.com/questions/52929034/convert-only-time-part-in-android-from-string-to-time/52929217#52929217) earlier with no attempt, got your answer, and now you're asking your next question with no attempt. Please provide a [mcve] this time, and show your own attempt! Plus I gave you an example of how to compare times on your earlier question... – achAmháin Oct 22 '18 at 13:10
  • From https://stackoverflow.com/questions/52929034/convert-only-time-part-in-android-from-string-to-time to this – forpas Oct 22 '18 at 13:10
  • If you want to check if a date is in a range of 2 other dates, try [this](https://stackoverflow.com/questions/494180/java-how-do-i-check-if-a-date-is-within-a-certain-range). – A. Wolf Oct 22 '18 at 13:11
  • 2
    Read the javadocs of [`LocalDateTime`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html), [`LocalDate`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) and [`LocalTime`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html) and maybe some examples, there are a lot of them in the www... – deHaar Oct 22 '18 at 13:14
  • I have this so far... – MTplus Oct 22 '18 at 13:15
  • Boolean InBetweenTime(Date currentTime, Date StartTime, Date EndTime) { Boolean result = false; Calendar calendar = Calendar.getInstance(); if ((currentTime.before(StartTime))&&(currentTime.after(EndTime))){ //time is inbetween } else { //time is not inbetween } return result; } – MTplus Oct 22 '18 at 13:15
  • I recommend you learn to use java.time, the modern Java date and time API. [Tutorial link](https://docs.oracle.com/javase/tutorial/datetime/). To use it on not-new Android, add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your project. – Ole V.V. Oct 22 '18 at 13:26
  • Assuming that start time is before end time, for current time to be between them it needs to be *after start* time and *before end* time, not the other way around. – Ole V.V. Oct 22 '18 at 13:29
  • I'm trying to stick to API level 15 for compability reasons and cannot use LocalTime, the above code would work if I only could compare the time part instead of the whole date/time. – MTplus Oct 22 '18 at 13:53
  • Though I have not got the experience I strongly believe that [the ThreeTenABP library that I already linked to once](https://github.com/JakeWharton/ThreeTenABP) is all you need to use `LocalTime` on API level 15. – Ole V.V. Oct 22 '18 at 17:09

1 Answers1

0

If what you need is to compare only time, ignoring date, then you should do something like this, so your 'date' part be equal in all three variables.

Boolean InBetweenTime(Date currentTime, Date startTime, Date endTime){
    Boolean result = false;

    //calendar for currentTime
    Calendar currentCal = Calendar.getInstance();
    currentCal.setTime(currentTime);
    //calendar for startTime
    Calendar startCal = Calendar.getInstance();
    startCal.setTime(startTime);
    //calendar for endTime
    Calendar endCal = Calendar.getInstance();
    endCal.setTime(endTime);
    //set corresponding date fields of startTime and endTime to be equal t ocurrentTime, so you compare only time fields
    startCal.set(currentCal.get(Calendar.YEAR), currentCal.get(Calendar.MONTH), currentCal.get(Calendar.DAY_OF_MONTH));
    endCal.set(currentCal.get(Calendar.YEAR), currentCal.get(Calendar.MONTH), currentCal.get(Calendar.DAY_OF_MONTH));
    //return true if between, false otherwise
    return (!currentTime.before(startCal.getTime())) && (!currentTime.after(endCal.getTime()));
}