0

I have time spans:

long dateStart = System.currentTimeMillis();
long dateEnd = System.currentTimeMillis() + 60*60*1000;

I want to check if mHour and mMinute lies between dateStart and dateEnd.

For example, mHour = 11 and mMinute = 22, so what I want is, dateStart < 11:22 < dateEnd

Here the date does not matter.

Another example:

2014/10/25 15:46 < 10:30 < 2014/10/26 : 15:45, the expected result will be true. 
Because, 2014/10/26 10:30 < 2014/10/26 : 15:45

A clarification:

the lower and upper bounds of dateStart and dateEnd cannot exceed 1 day span. So, if dateStart is 
2014/10/26 23:59, the most dateEnd would be 2014/10/27 23:58.

I have done this but it did not work out:

 compareTime(dateStart ,dateEnd ,mHour  ,mMinute );



 public static void compareTime(long time1, long time2, int mHour, int mMinute) throws ParseException {

    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(new Date(time2));

    Calendar calendar2 = Calendar.getInstance();
    calendar2.setTime(new Date(time1));
    calendar2.add(Calendar.DATE, 1);

    String someRandomTime = selectedHour + ":" + selectedminute;
    Date d = new SimpleDateFormat("HH:mm").parse(someRandomTime);
    Calendar calendar3 = Calendar.getInstance();
    calendar3.setTime(d);
    calendar3.add(Calendar.DATE, 1);

    Date x = calendar3.getTime();

    if (x.after(calendar1.getTime()) && x.before(calendar2.getTime()))
        System.out.println(true);

}
Co Koder
  • 2,021
  • 7
  • 31
  • 39
  • What time zone are you interested in? The question only makes sense in a particular time zone, as the same `Date` value represents different local times around the world. – Jon Skeet Oct 15 '14 at 06:08
  • @Tarek, you mean using some fake year, month and day – Co Koder Oct 15 '14 at 06:09
  • http://stackoverflow.com/questions/883060/how-can-i-determine-if-a-date-is-between-two-dates-in-java – Sivakumar Oct 15 '14 at 06:09
  • @JonSkeet, the time zone should be the same as dateStart and dateEnd. – Co Koder Oct 15 '14 at 06:12
  • @CoKoder: Those are just numbers - the type of `dateStart` and `dateEnd` is a `long`. What's the time zone of 12345? Neither `Date` nor `long` has the concept of a time zone. This is a *crucial* choice, and one that you really need to understand before you can write any sensible implementation. Your current code implicitly uses the system default time zone - but in many cases that would be entirely inappropriate. – Jon Skeet Oct 15 '14 at 06:22
  • Additionally, what would you want to do if `dateStart` and `dateEnd` are more than a day apart? In that case - leaving aside skipped times due to DST transitions - you'd *always* cover the given hour and minute, but you wouldn't be able to tell that just from the local times of start/end. – Jon Skeet Oct 15 '14 at 06:24
  • Can you add some examples with expected results? E.g. what is expected result for `2014/10/25 11:20 < 10:30 < 2014/10/27 : 15:45`? – Ilya Oct 15 '14 at 13:11
  • @Ilya, i have added another example. For this example, the expected result will be true. – Co Koder Oct 15 '14 at 14:08
  • How about this case: 2013/12/31 23:59 ---- 10:30 ----- 2014/01/01: 00:01 ------------- How do you want for this case? – LHA Oct 15 '14 at 14:12
  • @LocHa, i have tried to make it clear by updating the question, please check it out. For your example, the expected result will be false. – Co Koder Oct 15 '14 at 14:19

1 Answers1

1

It's simply to do with Joda-time library. compareTime method will looks like:

   /**
    * @param time1 dateFrom in millis. E.g. returned by System.currentTimeMillis()
    * @param time2 dateTo in millis
    * @param mHour hours in your time zone
    * @param mMinute minutes in your time zone
    */
   public static boolean compareTime(long time1, long time2, int mHour, int mMinute)  
   {
      final LocalTime timeFrom = new LocalTime(time1);
      final LocalTime timeTo = new LocalTime(time2);
      final LocalTime time = new LocalTime(mHour, mMinute);
      final boolean isSameDay = timeFrom.isBefore(timeTo); // it is correct, because (time2 - time1 < 24 hours)
      return isSameDay ? (time.isAfter(timeFrom) && time.isBefore(timeTo))
         :  (time.isAfter(timeFrom) || time.isBefore(timeTo));
   }
Ilya
  • 29,135
  • 19
  • 110
  • 158