Corn表达式与时间整点半点代码片

× 文章目录
  1. 1. Corn表达式
  2. 2. 判断时间是否整点或者半点执行

摘要:本文主要介绍了Corn表达式和判断时间是否半点或者整点执行的代码片。

Corn表达式

1
2
3
4
5
6
7
8
9
10
11
String exp="0/3 * * * * ? ";
//判断表达式是否有效
boolean valid = CronExpression.isValidExpression(exp);
//使用CronExpression生成时间序列
CronExpression cronExpression = new CronExpression(exp);
System.out.println(cronExpression.getNextValidTimeAfter(new Date()));
//使用CronSequenceGenerator生成时间序列
CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(exp);
Date nextTimePoint = cronSequenceGenerator.next(new Date());

判断时间是否整点或者半点执行

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
package com.xxx.xxx.utils;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* @Author xujin
*/
public class DateUtil {
/**
* 判断当前时间是半点
* @return
*/
public static Boolean judgeTimeIsHalf()
{
Boolean judgeTimeIsHalf=false;
Date date = new Date();
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
if (gc.get(gc.MINUTE)==30)
{
judgeTimeIsHalf= true;
}
return judgeTimeIsHalf;
}
/**
* 判断当前时间是半点
* @return
*/
public static boolean judgeTimeIsWhole()
{
Boolean judgeTimeIsWhole=false;
Date date = new Date();
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
if ( gc.get(gc.MINUTE)==0 )
{
judgeTimeIsWhole= true;
}
return judgeTimeIsWhole;
}
public static int getMinute()
{
Date date = new Date();
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
return gc.get(gc.MINUTE);
}
public static boolean quarztTime(int h,int m)
{
Boolean isQuarztTime=false;
Date date = new Date();
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
if((gc.get(gc.HOUR_OF_DAY)==h&&gc.get(gc.MINUTE)==m))
{
isQuarztTime= true;
}
return isQuarztTime;
}
public static String getHourAndMinute()
{
Date date = new Date();
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
String hour=String.valueOf(gc.get(gc.HOUR_OF_DAY));
String minute=String.valueOf(gc.get(gc.MINUTE));
return hour+minute;
}
public static void main(String[] args)
{
System.out.println(getHourAndMinute());
Calendar calendar=Calendar.getInstance();
System.out.println(
"现在是:"+
calendar.get(GregorianCalendar.YEAR)+"年"+
(calendar.get(GregorianCalendar.MONTH)+1)+"月"+
calendar.get(GregorianCalendar.DAY_OF_MONTH)+"日"+
calendar.get(GregorianCalendar.HOUR)+"时"+
calendar.get(GregorianCalendar.MINUTE)+"分"+
calendar.get(GregorianCalendar.SECOND)+"秒"
);
}
}
如果您觉得文章不错,可以打赏我喝一杯咖啡!