forked from chenssy89/jutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimestampUtils.java
More file actions
89 lines (82 loc) · 2.1 KB
/
TimestampUtils.java
File metadata and controls
89 lines (82 loc) · 2.1 KB
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
package com.JUtils.date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* TimeStamp工具类,提供TimeStamp与String、Date的转换
*
* @author chenssy
* @date 2016-09-24
* @since 1.0.0
*/
public class TimestampUtils {
/**
* String转换为TimeStamp
* @param value
* 待转换的String,格式必须为 yyyy-mm-dd hh:mm:ss[.f...] 这样的格式,中括号表示可选,否则报错
* @return java.sql.Timestamp
*
* @author chenssy
* @date 2016-09-24
* @since v1.0.0
*/
public static Timestamp string2Timestamp(String value){
if(value == null && !"".equals(value.trim())){
return null;
}
Timestamp ts = new Timestamp(System.currentTimeMillis());
ts = Timestamp.valueOf(value);
return ts;
}
/**
* 将Timestamp 转换为String类型,format为null则使用默认格式 yyyy-MM-dd HH:mm:ss
*
* @param value
* 待转换的Timestamp
* @param format
* String的格式
* @return java.lang.String
*
* @author chenssy
* @date 2016-09-24
* @since v1.0.0
*/
public static String timestamp2String(Timestamp value,String format){
if(null == value){
return "";
}
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
return sdf.format(value);
}
/**
* Date转换为Timestamp
*
* @param date
* 待转换的Date
* @return java.sql.Timestamp
*
* @author chenssy
* @date 2016-09-24
* @since v1.0.0
*/
public static Timestamp date2Timestamp(Date date){
if(date == null){
return null;
}
return new Timestamp(date.getTime());
}
/**
* Timestamp转换为Date
*
* @param time
* 待转换的Timestamp
* @return java.util.Date
*
* @author chenssy
* @date 2016-09-24
* @since v1.0.0
*/
public static Date timestamp2Date(Timestamp time){
return time == null ? null : time;
}
}