-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateUtil.java
85 lines (76 loc) · 2.1 KB
/
DateUtil.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
package cn.piesat.datautilservice.DateUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @description:时间工具类
* @createTime: 2019.4.17 13:41
* @author: XD
* @version: 1.0
*/
public class DateUtil {
/**
* 将日期按照yyyy-MM-dd HH:mm:ss的格式转换为字符串
*
* @param date
* @return
*/
public static String dateToStr(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(date);
return time;
}
/**
* 将日期按照指定的格式转换为字符串
*
* @param date
* @param format
* @return
*/
public static String dateToStrByFormat(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
String time = sdf.format(date);
return time;
}
/**
* 将yyyy-MM-dd HH:mm:ss格式的字符串转化为日期
*
* @param str
* @return
* @throws ParseException
*/
public static Date strToDate(String str) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(str);
return date;
}
/**
* 将字符串按照指定格式转化为日期
*
* @param str
* @param format
* @return
* @throws ParseException
*/
public static Date strToDateByFormat(String str, String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(str);
return date;
}
/**
* 将2019-06-13T19:34:28.000+0000 格式的时间转换为yyyy-MM-dd HH:mm:ss
*/
public static String mysqlTimetoStr(String mysqlTime) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sssZ");
Date date = formatter.parse(mysqlTime);
return dateToStr(date);
}
/**
* 获得当前时间
*
* @return Date
*/
public static Date getNowTime() {
return new Date();
}
}