时间日期转换工具类:SimpleDateFormat
比较时间工具类:Calendar
直接上代码:
@Test
public void test() throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
Date time = df.parse("22:00:00");
Date begin = df.parse("7:00:00");
Date end = df.parse("22:00:00");
Calendar nowTime = Calendar.getInstance();
nowTime.setTime(time);
Calendar beginTime = Calendar.getInstance();
beginTime.setTime(begin);
Calendar endTime = Calendar.getInstance();
endTime.setTime(end);
// System.out.println("endTime:" + endTime);
if ((nowTime.before(endTime) && nowTime.after(beginTime)) || nowTime.equals(endTime) || nowTime.equals(beginTime)) {
System.out.println("在该时间段内");
} else {
System.out.println("不在");
}
}