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);
}