判斷當(dāng)前時(shí)間是否在時(shí)間段內(nèi)(可隔天驼鹅,也可不隔天)
public class TimeIntervalUtil {
/**
* 判斷時(shí)間是否在某個(gè)時(shí)間段內(nèi)
*
* @param nowTime 需要判斷的時(shí)間,形如23:30:00 (可為空)
* @param startTime 時(shí)間段開(kāi)始時(shí)間,形如22:30:00
* @param endTime 時(shí)間段結(jié)束時(shí)間,形如06:00:00
* @return boolean
*/
public static boolean isInTimeRange(String nowTime, String startTime, String endTime) {
// 時(shí)間為空,默認(rèn)賦值當(dāng)前時(shí)間(時(shí)分秒)
if (StrUtil.isBlank(nowTime)) {
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("HH:mm");
nowTime = LocalDateTime.now().format(pattern);
}
int set = Integer.valueOf(nowTime.replaceAll(":", ""));
int begin = Integer.valueOf(startTime.replaceAll(":", ""));
int end = Integer.valueOf(endTime.replaceAll(":", ""));
if (begin > end) {
return set < end || set > begin;
} else {
return set > begin && set < end;
}
}
public static void main(String[] args) throws ParseException {
boolean inTimeRange = isInTimeRange("11:50", "20:00", "08:00");
System.out.println(inTimeRange);
String str = DateTime.now().toTimeStr();
System.out.println(str);
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("HH:mm");
String format = LocalDateTime.now().format(pattern);
System.out.println(format);
boolean inTimeRange1 = isInTimeRange(format, "20:00", "08:00");
System.out.println(inTimeRange1);
DateTime dateTime = DateUtil.endOfDay(DateUtil.date());
System.out.println(dateTime);
}
}