package com.liying.tiger.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Test { public static void main(String[] args) throws ParseException { String format = "HH:mm:ss";
//Date nowTime = new SimpleDateFormat(format).parse("09:27:00"); Date now = new Date();
DateFormat dateFormat = DateFormat.getTimeInstance();//獲取時分秒
//獲得當前時間的時分秒
String nowStr = dateFormat.format(now);
Date nowTime = new SimpDateFormat(format).parse(nowStr);
Date startTime = new SimpleDateFormat(format).parse("09:27:00"); Date endTime = new SimpleDateFormat(format).parse("09:27:59"); System.out.println(isEffectiveDate(nowTime, startTime, endTime)); } /** * 判斷當前時間是否在[startTime, endTime]區間,注意時間格式要一致 * * @param nowTime 當前時間 * @param startTime 開始時間 * @param endTime 結束時間 * @return * @author jqlin */ public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) { if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) { return true; } Calendar date = Calendar.getInstance(); date.setTime(nowTime); Calendar begin = Calendar.getInstance(); begin.setTime(startTime); Calendar end = Calendar.getInstance(); end.setTime(endTime); if (date.after(begin) && date.before(end)) { return true; } else { return false; } } }
轉載至:https://www.cnblogs.com/linjiqin/p/8119338.htmlhtml