-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtils.java
More file actions
89 lines (45 loc) · 1.54 KB
/
DateUtils.java
File metadata and controls
89 lines (45 loc) · 1.54 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.chen.api.util;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author : chen weijie
* @Date: 2019-05-16 14:46
*/
public class DateUtils {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 1.Calendar 转化 String
Calendar calendat = Calendar.getInstance();
String dateStr = sdf.format(calendat.getTime());
// 2.String 转化Calendar
String str = "2012-5-27";
Date date = null;
try {
date = sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 3.Date 转化String
dateStr = sdf.format(new Date());
// 4.String 转化Date
str = "2012-5-27";
date = sdf.parse(str);
// 5.Date 转化Calendar
calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date());
// 6.Calendar转化Date
calendar = Calendar.getInstance();
date = calendar.getTime();
// 7.String 转成 Timestamp
Timestamp ts = Timestamp.valueOf("2012-1-14 08:11:00");
// 8.Date 转 TimeStamp
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(new Date());
ts = Timestamp.valueOf(time);
}
}