forked from zilianliuxue/AndroidStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolDateTime.java
More file actions
312 lines (277 loc) · 7.3 KB
/
Copy pathToolDateTime.java
File metadata and controls
312 lines (277 loc) · 7.3 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package com.richerpay.ryshop.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.richerpay.ryshop.log.KLog;
/**
* 时间日期格式化工具类
*
*/
public class ToolDateTime {
/** 日期格式:yyyy-MM-dd HH:mm:ss **/
public static final String DF_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
/** 日期格式:yyyy-MM-dd HH:mm **/
public static final String DF_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
/** 日期格式:yyyy-MM-dd **/
public static final String DF_YYYY_MM_DD = "yyyy-MM-dd";
/** 日期格式:HH:mm:ss **/
public static final String DF_HH_MM_SS = "HH:mm:ss";
/** 日期格式:HH:mm **/
public static final String DF_HH_MM = "HH:mm";
private final static long MINUTE = 60 * 1000;// 1分钟
private final static long HOUR = 60 * MINUTE;// 1小时
private final static long DAY = 24 * HOUR;// 1天
private final static long MONTH = 31 * DAY;// 月
private final static long YEAR = 12 * MONTH;// 年
/** Log输出标识 **/
private static final String TAG = ToolDateTime.class.getSimpleName();
/**
* 将日期格式化成友好的字符串:几分钟前、几小时前、几天前、几月前、几年前、刚刚
*
* @param date
* @return
*/
public static String formatFriendly(Date date) {
if (date == null) {
return null;
}
long diff = new Date().getTime() - date.getTime();
long r = 0;
if (diff > YEAR) {
r = (diff / YEAR);
return r + "年前";
}
if (diff > MONTH) {
r = (diff / MONTH);
return r + "个月前";
}
if (diff > DAY) {
r = (diff / DAY);
return r + "天前";
}
if (diff > HOUR) {
r = (diff / HOUR);
return r + "个小时前";
}
if (diff > MINUTE) {
r = (diff / MINUTE);
return r + "分钟前";
}
return "刚刚";
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param dateL
* 日期
* @return
*/
public static String formatDateTime(long dateL) {
SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
Date date = new Date(dateL);
return sdf.format(date);
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param dateL
* 日期
* @return
*/
public static String formatDateTime(long dateL, String formater) {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(new Date(dateL));
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param dateL
* 日期
* @return
*/
public static String formatDateTime(Date date, String formater) {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(date);
}
/**
* 将日期字符串转成日期
*
* @param strDate
* 字符串日期
* @return java.util.date日期类型
*/
public static Date parseDate(String strDate) {
DateFormat dateFormat = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
Date returnDate = null;
try {
returnDate = dateFormat.parse(strDate);
} catch (ParseException e) {
KLog.v(TAG, "parseDate failed !");
}
return returnDate;
}
/**
* 获取系统当前日期
*
* @return
*/
public static Date gainCurrentDate() {
return new Date();
}
/**
* 验证日期是否比当前日期早
*
* @param target1
* 比较时间1
* @param target2
* 比较时间2
* @return true 则代表target1比target2晚或等于target2,否则比target2早
*/
public static boolean compareDate(Date target1, Date target2) {
boolean flag = false;
try {
String target1DateTime = ToolDateTime.formatDateTime(target1,
DF_YYYY_MM_DD_HH_MM_SS);
String target2DateTime = ToolDateTime.formatDateTime(target2,
DF_YYYY_MM_DD_HH_MM_SS);
if (target1DateTime.compareTo(target2DateTime) <= 0) {
flag = true;
}
} catch (Exception e1) {
KLog.e("比较失败,原因:" + e1.getMessage());
}
return flag;
}
/**
* 对日期进行增加操作
*
* @param target
* 需要进行运算的日期
* @param hour
* 小时
* @return
*/
public static Date addDateTime(Date target, double hour) {
if (null == target || hour < 0) {
return target;
}
return new Date(target.getTime() + (long) (hour * 60 * 60 * 1000));
}
/**
* 对日期进行相减操作
*
* @param target
* 需要进行运算的日期
* @param hour
* 小时
* @return
*/
public static Date subDateTime(Date target, double hour) {
if (null == target || hour < 0) {
return target;
}
return new Date(target.getTime() - (long) (hour * 60 * 60 * 1000));
}
private static SimpleDateFormat second = new SimpleDateFormat(
"yy-MM-dd hh:mm:ss");
private static SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat detailDay = new SimpleDateFormat("yyyy年MM月dd日");
private static SimpleDateFormat fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
private static SimpleDateFormat tempTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat excelDate = new SimpleDateFormat("yyyy/MM/dd");
/**
* 格式化excel中的时间
* @param date
* @return
*/
public static String formatDateForExcelDate(Date date) {
return excelDate.format(date);
}
/**
* 将日期格式化作为文件名
* @param date
* @return
*/
public static String formatDateForFileName(Date date) {
return fileName.format(date);
}
/**
* 格式化日期(精确到秒)
*
* @param date
* @return
*/
public static String formatDateSecond(Date date) {
return second.format(date);
}
/**
* 格式化日期(精确到秒)
*
* @param date
* @return
*/
public static String tempDateSecond(Date date) {
return tempTime.format(date);
}
public static Date tempDateSecond(String str) {
try {
return tempTime.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return new Date();
}
/**
* 格式化日期(精确到天)
*
* @param date
* @return
*/
public static String formatDateDay(Date date) {
return day.format(date);
}
/**
* 格式化日期(精确到天)
*
* @param date
* @return
*/
public static String formatDateDetailDay(Date date) {
return detailDay.format(date);
}
/**
* 将double类型的数字保留两位小数(四舍五入)
*
* @param number
* @return
*/
public static String formatNumber(double number) {
DecimalFormat df = new DecimalFormat();
df.applyPattern("#0.00");
return df.format(number);
}
/**
* 将字符串转换成日期
*
* @param date
* @return
* @throws Exception
*/
public static Date formateDate(String date) throws Exception {
return day.parse(date);
}
/**
* 将字符日期转换成Date
* @param date
* @return
* @throws Exception
*/
public static Date parseStringToDate(String date) throws Exception {
return day.parse(date);
}
public static String formatDoubleNumber(double number) {
DecimalFormat df = new DecimalFormat("#");
return df.format(number);
}
}