需求:當時間在凌晨0點至0點5分之間程序不執行。
也就是實現判斷當前時間點是否在00:00:00至00:05:00之間
方法:
Java代碼 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/** * 判斷時間是否在時間段內 * * @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 ; } } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!