Java 斷定一個時光能否在另外一個時光段內。本站提示廣大學習愛好者:(Java 斷定一個時光能否在另外一個時光段內)文章只能為提供參考,不一定能成為您想要的結果。以下是Java 斷定一個時光能否在另外一個時光段內正文
需求:其時間在清晨0點至0點5分之間法式不履行。
也就是完成斷定以後時光點能否在00:00:00至00:05:00之間
辦法:
Java代碼 :
/**
* 斷定時光能否在時光段內 *
* @param date
* 以後時光 yyyy-MM-dd HH:mm:ss
* @param strDateBegin
* 開端時光 00:00:00
* @param strDateEnd
* 停止時光 00:05:00
* @return
*/
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;
}
}
感激浏覽,願望能贊助到年夜家,感謝年夜家對本站的支撐!