public static boolean isInDate(Date date, String strDateBegin, String strDateEnd) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String strDate = sdf.format(date); // 截取當前時間時分秒 int strDateH = Integer.parseInt(strDate.substring(11, 13)); int strDateM = Integer.parseInt(strDate.substring(14, 16)); int strDateS = Integer.parseInt(strDate.substring(17, 19)); // 截取開始時間時分秒 int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2)); int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5)); int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8)); // 截取結束時間時分秒 int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2)); int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5)); int strDateEndS = Integer.parseInt(strDateEnd.substring(6, 8)); if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) { // 當前時間小時數在開始時間和結束時間小時數之間 if (strDateH > strDateBeginH && strDateH < strDateEndH) { return true; // 當前時間小時數等於開始時間小時數,分鍾數在開始和結束之間 } else if (strDateH == strDateBeginH && strDateM >= strDateBeginM && strDateM <= strDateEndM) { return true; // 當前時間小時數等於開始時間小時數,分鍾數等於開始時間分鍾數,秒數在開始和結束之間 } else if (strDateH == strDateBeginH && strDateM == strDateBeginM && strDateS >= strDateBeginS && strDateS <= strDateEndS) { return true; } // 當前時間小時數大等於開始時間小時數,等於結束時間小時數,分鍾數小等於結束時間分鍾數 else if (strDateH >= strDateBeginH && strDateH == strDateEndH && strDateM <= strDateEndM) { return true; // 當前時間小時數大等於開始時間小時數,等於結束時間小時數,分鍾數等於結束時間分鍾數,秒數小等於結束時間秒數 } else if (strDateH >= strDateBeginH && strDateH == strDateEndH && strDateM == strDateEndM && strDateS <= strDateEndS) { return true; } else { return false; } } else { return false; } }