Java determines whether one time is in another time period
- 2020-05-10 18:09:56
- OfStack
requirement: the program does not execute when the time is between 0 a.m. and 0.5 a.m.
That is, the implementation determines whether the current time point is between 00:00:00 and 00:05:00
Methods:
Java code:
/**
* Determine if the time is within the time frame *
* @param date
* The current time yyyy-MM-dd HH:mm:ss
* @param strDateBegin
* The start time 00:00:00
* @param strDateEnd
* The end of time 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);
// Intercept the current time
int strDateH = Integer.parseInt(strDate.substring(11, 13));
int strDateM = Integer.parseInt(strDate.substring(14, 16));
int strDateS = Integer.parseInt(strDate.substring(17, 19));
// Intercept the start time at the minute
int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));
int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));
int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8));
// Intercept the end time at the minute second
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)) {
// The current time number of hours is between the start time and the end time number of hours
if (strDateH > strDateBeginH && strDateH < strDateEndH) {
return true;
// The number of hours in the current time is equal to the number of hours in the start time, and the number of minutes is between the start time and the end time
} else if (strDateH == strDateBeginH && strDateM >= strDateBeginM
&& strDateM <= strDateEndM) {
return true;
// The number of current time hours is equal to the number of start time hours, the number of minutes is equal to the number of start time minutes, and the number of seconds is between the start and the end
} else if (strDateH == strDateBeginH && strDateM == strDateBeginM
&& strDateS >= strDateBeginS && strDateS <= strDateEndS) {
return true;
}
// The number of hours of the current time is equal to the number of hours of the start time, is equal to the number of hours of the end time, and the number of minutes is equal to the number of minutes of the end time
else if (strDateH >= strDateBeginH && strDateH == strDateEndH
&& strDateM <= strDateEndM) {
return true;
// The number of hours of the current time is equal to the number of hours of the start time, the number of hours of the end time, the number of minutes is equal to the number of minutes of the end time, and the number of seconds is equal to the number of seconds of the end time
} else if (strDateH >= strDateBeginH && strDateH == strDateEndH
&& strDateM == strDateEndM && strDateS <= strDateEndS) {
return true;
} else {
return false;
}
} else {
return false;
}
}
Thank you for reading, I hope to help you, thank you for your support of this site!