try this
int from = 2300;
int to = 800;
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);
use this method:
String dateFormat = "HH:mm:ss";
String startTime= "20:20:20";
String endTime= "12:12:12";
String currentTime = new SimpleDateFormat(dateFormat).format(new Date());
Calendar cStart = setTimeToCalendar(dateFormat, startTime, false);
Calendar cEnd = setTimeToCalendar(dateFormat, endTime, true);
Calendar cNow = setTimeToCalendar(dateFormat, currentTime, true);
Date curDate = cNow.getTime();
if (curDate.after(cStart.getTime()) && curDate.before(cEnd.getTime())) {
System.out.println("Date is in range");
} else {
System.out.println("Date is out of range");
}
String timeFrom = "12:00:00";
String timeTo = "15:00:00";
String timePoint="14:30:20";
Long timeFromVal = Time.parse(timeFrom);
Long timeToVal = Time.parse(timeTo);
Long timePointVal = Time.parse(timePoint);
if(timePointVal>timeFromVal && timePointVal<timeToVal)
Log.i("Time report ","Time is Valid.");
For example:
try {
String string1 = "20:11:13";
Date time1 = new SimpleDateFormat("HH:mm:ss").parse(string1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
String string2 = "14:49:00";
Date time2 = new SimpleDateFormat("HH:mm:ss").parse(string2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);
String someRandomTime = "01:00:00";
Date d = new SimpleDateFormat("HH:mm:ss").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())) {
//checkes whether the current time is between 14:49:00 and 20:11:13.
System.out.println(true);
}
} catch (ParseException e) {
e.printStackTrace();
}
上面的 code, 滿有趣的,我把變數內容印出來,他的日期時間居然是第 1天的日期(Thu Jan 01 12:34:56 CST 1970)時間,如果日期是空的的時候。