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

Commit 7622d79

Browse files
committed
add method for parsing time
1 parent 1c198a5 commit 7622d79

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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

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

3+
import cn.hutool.core.date.DateUtil;
4+
import com.zhazhapan.modules.constant.ValueConsts;
5+
import org.junit.Assert;
6+
37
import java.sql.Time;
48
import java.sql.Timestamp;
59
import java.text.ParseException;
@@ -17,6 +21,36 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
1721

1822
private DateUtils() {}
1923

24+
/**
25+
* 解析时间字符串
26+
*
27+
* @param time 时间
28+
*
29+
* @return {@link Time}
30+
*
31+
* @since 1.1.1
32+
*/
33+
public static Time parseTime(String time) {
34+
Assert.assertTrue("time is required, but value is empty", Checker.isNotEmpty(time));
35+
String[] times = time.split(ValueConsts.COLON);
36+
String timeStr = formatTimeString(times[0]) + ":" + (times.length > 1 ? formatTimeString(times[1]) : "00") +
37+
":" + (times.length > 2 ? formatTimeString(times[2]) : "00");
38+
return new Time(DateUtil.parseTime(timeStr).getTime());
39+
}
40+
41+
/**
42+
* 格式化字符串
43+
*
44+
* @param time 时间
45+
*
46+
* @return {@link String}
47+
*
48+
* @since 1.1.1
49+
*/
50+
private static String formatTimeString(String time) {
51+
return time.length() > 0 ? (time.length() == 1 ? "0" : "") + time : "00";
52+
}
53+
2054
/**
2155
* 将日期转换成时间戳
2256
*

0 commit comments

Comments
 (0)