Skip to content
This repository was archived by the owner on Jan 24, 2019. It is now read-only.

Commit e51a9a8

Browse files
committed
add time compared method
1 parent b23bf86 commit e51a9a8

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

src/main/java/com/zhazhapan/util/DateUtils.java

+84
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.zhazhapan.util;
22

3+
import java.sql.Time;
34
import java.sql.Timestamp;
45
import java.text.ParseException;
56
import java.util.Calendar;
@@ -14,6 +15,72 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
1415

1516
private DateUtils() {}
1617

18+
/**
19+
* 第一个值是否大于第二个值
20+
*
21+
* @param big 第一个值
22+
* @param small 第二个值
23+
*
24+
* @return {@link Boolean}
25+
*
26+
* @since 1.1.0
27+
*/
28+
public static boolean greatThan(Time big, Time small) {
29+
return compare(big, small, CompareWay.GT);
30+
}
31+
32+
/**
33+
* 第一个值是否等于第二个值
34+
*
35+
* @param man 第一个值
36+
* @param wife 第二个值
37+
*
38+
* @return {@link Boolean}
39+
*
40+
* @since 1.1.0
41+
*/
42+
public static boolean equals(Time man, Time wife) {
43+
return compare(man, wife, CompareWay.EQ);
44+
}
45+
46+
/**
47+
* 第一个值是否小于第二个值
48+
*
49+
* @param small 第一个值
50+
* @param big 第二个值
51+
*
52+
* @return {@link Boolean}
53+
*
54+
* @since 1.1.0
55+
*/
56+
public static boolean lessThan(Time small, Time big) {
57+
return compare(small, big, CompareWay.LT);
58+
}
59+
60+
/**
61+
* 将第一个值和第二个值进行比较
62+
*
63+
* @param aValue 第一个值
64+
* @param bValue 第二个值
65+
* @param compareWay 比较方式
66+
*
67+
* @return {@link Boolean}
68+
*
69+
* @since 1.1.0
70+
*/
71+
public static boolean compare(Time aValue, Time bValue, CompareWay compareWay) {
72+
String value = Formatter.shortTimeToString(aValue);
73+
int result = value.compareTo(Formatter.shortTimeToString(bValue));
74+
switch (compareWay) {
75+
case EQ:
76+
return result == 0;
77+
case GT:
78+
return result > 0;
79+
default:
80+
return result < 0;
81+
}
82+
}
83+
1784
/**
1885
* 获取当前年份
1986
*
@@ -370,4 +437,21 @@ public static Date add(Date date, int field, int amount) {
370437
calendar.add(field, amount);
371438
return calendar.getTime();
372439
}
440+
441+
public enum CompareWay {
442+
/**
443+
* 进行小于
444+
*/
445+
LT,
446+
447+
/**
448+
* 进行等于比较
449+
*/
450+
EQ,
451+
452+
/**
453+
* 进行大于比较
454+
*/
455+
GT
456+
}
373457
}

0 commit comments

Comments
 (0)