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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class DateUtils {
/**定义常量**/ public static final String DATE_JFP_STR= "yyyyMM" ;
public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss" ;
public static final String DATE_SMALL_STR = "yyyy-MM-dd" ;
public static final String DATE_KEY_STR = "yyMMddHHmmss" ;
/** * 使用预设格式提取字符串日期 * @param strDate 日期字符串 * @return */ public static Date parse(String strDate) {
return parse(strDate,DATE_FULL_STR);
} /** * 使用用户格式提取字符串日期 * @param strDate 日期字符串 * @param pattern 日期格式 * @return */ public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace(); return null ;
} } /** * 两个时间比较 * @param date * @return */ public static int compareDateWithNow(Date date1){
Date date2 = new Date();
int rnum =date1.compareTo(date2);
return rnum;
} /** * 两个时间比较(时间戳比较) * @param date * @return */ public static int compareDateWithNow( long date1){
long date2 = dateToUnixTimestamp();
if (date1>date2){
return 1 ;
} else if (date1<date2){
return - 1 ;
} else {
return 0 ;
} } /** * 获取系统当前时间 * @return */ public static String getNowTime() {
SimpleDateFormat df = new SimpleDateFormat(DATE_FULL_STR);
return df.format( new Date());
} /** * 获取系统当前时间 * @return */ public static String getNowTime(String type) {
SimpleDateFormat df = new SimpleDateFormat(type);
return df.format( new Date());
} /** * 获取系统当前计费期 * @return */ public static String getJFPTime() {
SimpleDateFormat df = new SimpleDateFormat(DATE_JFP_STR);
return df.format( new Date());
} /** * 将指定的日期转换成Unix时间戳 * @param String date 需要转换的日期 yyyy-MM-dd HH:mm:ss * @return long 时间戳 */ public static long dateToUnixTimestamp(String date) {
long timestamp = 0 ;
try {
timestamp = new SimpleDateFormat(DATE_FULL_STR).parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace(); } return timestamp;
} /** * 将指定的日期转换成Unix时间戳 * @param String date 需要转换的日期 yyyy-MM-dd * @return long 时间戳 */ public static long dateToUnixTimestamp(String date, String dateFormat) {
long timestamp = 0 ;
try {
timestamp = new SimpleDateFormat(dateFormat).parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace(); } return timestamp;
} /** * 将当前日期转换成Unix时间戳 * @return long 时间戳 */ public static long dateToUnixTimestamp() {
long timestamp = new Date().getTime();
return timestamp;
} /** * 将Unix时间戳转换成日期 * @param long timestamp 时间戳 * @return String 日期字符串 */ public static String unixTimestampToDate( long timestamp) {
SimpleDateFormat sd = new SimpleDateFormat(DATE_FULL_STR);
sd.setTimeZone(TimeZone.getTimeZone( "GMT+8" ));
return sd.format( new Date(timestamp));
} } |
本文转自 梦朝思夕 51CTO博客,原文链接:http://blog.51cto.com/qiangmzsx/1305082