DateUtil.java
2.43 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package io.mycat.util;
import java.text.ParseException;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
/**
* 使用joda解析date,可以得到date的year,month,day等字段值
* @author CrazyPig
*
*/
public class DateUtil {
public static final String DEFAULT_DATE_PATTERN = "YYYY-MM-dd HH:mm:ss";
public static final String DATE_PATTERN_FULL = "YYYY-MM-dd HH:mm:ss.SSSSSS";
public static final String DATE_PATTERN_ONLY_DATE = "YYYY-MM-dd";
public static final String DEFAULT_TIME_PATTERN = "HHH:mm:ss";
public static final String TIME_PATTERN_FULL = "HHH:mm:ss.SSSSSS";
/**
* 根据日期字符串解析得到date类型日期
* @param dateStr
* @return
* @throws ParseException
*/
public static Date parseDate(String dateStr) throws ParseException {
return parseDate(dateStr, DEFAULT_DATE_PATTERN);
}
/**
* 根据日期字符串和日期格式解析得到date类型日期
* @param dateStr
* @param datePattern
* @return
* @throws ParseException
*/
public static Date parseDate(String dateStr, String datePattern) throws ParseException {
DateTime dt = DateTimeFormat.forPattern(datePattern).parseDateTime(dateStr);
return dt.toDate();
}
/**
* 获取date对象年份
* @param date
* @return
*/
public static int getYear(Date date) {
DateTime dt = new DateTime(date);
return dt.getYear();
}
/**
* 获取date对象月份
* @param date
* @return
*/
public static int getMonth(Date date) {
DateTime dt = new DateTime(date);
return dt.getMonthOfYear();
}
/**
* 获取date对象天数
* @param date
* @return
*/
public static int getDay(Date date) {
DateTime dt = new DateTime(date);
return dt.getDayOfMonth();
}
/**
* 获取date对象小时数
* @param date
* @return
*/
public static int getHour(Date date) {
DateTime dt = new DateTime(date);
return dt.getHourOfDay();
}
/**
* 获取date对象分钟数
* @param date
* @return
*/
public static int getMinute(Date date) {
DateTime dt = new DateTime(date);
return dt.getMinuteOfHour();
}
/**
* 获取date对象秒数
* @param date
* @return
*/
public static int getSecond(Date date) {
DateTime dt = new DateTime(date);
return dt.getSecondOfMinute();
}
/**
* 获取date对象毫秒数
* @param date
* @return
*/
public static int getMicroSecond(Date date) {
DateTime dt = new DateTime(date);
return dt.getMillisOfSecond();
}
}