Java 判斷一個時間是否在另一個時間段內(nèi)
更新時間:2016年10月14日 16:33:21 作者:l4432321
這篇文章主要介紹了Java 判斷一個時間是否在另一個時間段內(nèi)的相關(guān)資料,需要的朋友可以參考下
需求:當時間在凌晨0點至0點5分之間程序不執(zhí)行。
也就是實現(xiàn)判斷當前時間點是否在00:00:00至00:05:00之間
方法:
Java代碼 :
/**
* 判斷時間是否在時間段內(nèi) *
* @param date
* 當前時間 yyyy-MM-dd HH:mm:ss
* @param strDateBegin
* 開始時間 00:00:00
* @param strDateEnd
* 結(jié)束時間 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));
// 截取結(jié)束時間時分秒
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)) {
// 當前時間小時數(shù)在開始時間和結(jié)束時間小時數(shù)之間
if (strDateH > strDateBeginH && strDateH < strDateEndH) {
return true;
// 當前時間小時數(shù)等于開始時間小時數(shù),分鐘數(shù)在開始和結(jié)束之間
} else if (strDateH == strDateBeginH && strDateM >= strDateBeginM
&& strDateM <= strDateEndM) {
return true;
// 當前時間小時數(shù)等于開始時間小時數(shù),分鐘數(shù)等于開始時間分鐘數(shù),秒數(shù)在開始和結(jié)束之間
} else if (strDateH == strDateBeginH && strDateM == strDateBeginM
&& strDateS >= strDateBeginS && strDateS <= strDateEndS) {
return true;
}
// 當前時間小時數(shù)大等于開始時間小時數(shù),等于結(jié)束時間小時數(shù),分鐘數(shù)小等于結(jié)束時間分鐘數(shù)
else if (strDateH >= strDateBeginH && strDateH == strDateEndH
&& strDateM <= strDateEndM) {
return true;
// 當前時間小時數(shù)大等于開始時間小時數(shù),等于結(jié)束時間小時數(shù),分鐘數(shù)等于結(jié)束時間分鐘數(shù),秒數(shù)小等于結(jié)束時間秒數(shù)
} else if (strDateH >= strDateBeginH && strDateH == strDateEndH
&& strDateM == strDateEndM && strDateS <= strDateEndS) {
return true;
} else {
return false;
}
} else {
return false;
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Java基于ShardingSphere實現(xiàn)分庫分表的實例詳解
ShardingSphere?已于2020年4月16日成為?Apache?軟件基金會的頂級項目,?它們均提供標準化的數(shù)據(jù)水平擴展、分布式事務(wù)和分布式治理等功能,可適用于如?Java?同構(gòu)、異構(gòu)語言、云原生等各種多樣化的應(yīng)用場景,對ShardingSphere分庫分表相關(guān)知識感興趣的朋友一起看看吧2022-03-03
Java基礎(chǔ)知識精通二維數(shù)組的應(yīng)用
為了方便組織各種信息,計算機常將信息以表的形式進行組織,然后再以行和列的形式呈現(xiàn)出來。二維數(shù)組的結(jié)構(gòu)決定了其能非常方便地表示計算機中的表,以第一個下標表示元素所在的行,第二個下標表示元素所在的列。下面簡單了解一下二維數(shù)組,包括數(shù)組的聲明和初始化2022-04-04
Java并發(fā)工具類之CountDownLatch詳解
這篇文章主要介紹了Java并發(fā)工具類之CountDownLatch詳解,CountDownLatch可以使一個獲多個線程等待其他線程各自執(zhí)行完畢后再執(zhí)行,CountDownLatch可以解決那些一個或者多個線程在執(zhí)行之前必須依賴于某些必要的前提業(yè)務(wù)先執(zhí)行的場景,需要的朋友可以參考下2023-12-12
java 通過cmd 調(diào)用命令啟動tomcat的操作
這篇文章主要介紹了java 通過cmd 調(diào)用命令啟動tomcat的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Spring?Cloud?Alibaba使用Nacos作為注冊中心和配置中心
這篇文章主要為大家介紹了Spring?Cloud?Alibaba使用Nacos作為注冊中心和配置中心的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06

