Android:时间工具类 -- 时间戳转换成日期,日期转换成时间戳

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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
  public class DateUtils {

public static String getTodayDateTime() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
Locale.getDefault());
return format.format(new Date());
}

/**
* 掉此方法输入所要转换的时间输入例如("2014年06月14日16时09分00秒")返回时间戳
*
* @param time
* @return
*/
public String data(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒",
Locale.CHINA);
Date date;
String times = null;
try {
date = sdr.parse(time);
long l = date.getTime();
String stf = String.valueOf(l);
times = stf.substring(0, 10);
} catch (Exception e) {
e.printStackTrace();
}
return times;
}

public static String getTodayDateTimes() {
SimpleDateFormat format = new SimpleDateFormat("MM月dd日",
Locale.getDefault());
return format.format(new Date());
}

/**
* 获取当前时间
*
* @return
*/
public static String getCurrentTime_Today() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
return sdf.format(new java.util.Date());
}

/**
* 调此方法输入所要转换的时间输入例如("2014-06-14-16-09-00")返回时间戳
*
* @param time
* @return
*/
public static String dataOne(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss",
Locale.CHINA);
Date date;
String times = null;
try {
date = sdr.parse(time);
long l = date.getTime();
String stf = String.valueOf(l);
times = stf.substring(0, 10);
} catch (Exception e) {
e.printStackTrace();
}
return times;
}

public static String getTimestamp(String time, String type) {
SimpleDateFormat sdr = new SimpleDateFormat(type, Locale.CHINA);
Date date;
String times = null;
try {
date = sdr.parse(time);
long l = date.getTime();
String stf = String.valueOf(l);
times = stf.substring(0, 10);
} catch (Exception e) {
e.printStackTrace();
}
return times;
}

/**
* 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014年06月14日16时09分00秒")
*
* @param time
* @return
*/
public static String times(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;

}

/**
* 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014-06-14 16:09:00")
*
* @param time
* @return
*/
public static String timedate(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;

}

/**
* 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014年06月14日16:09")
*
* @param time
* @return
*/
public static String timet(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;

}

/**
* @param time斜杠分开
* @return
*/
public static String timeslash(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy/MM/dd,HH:mm");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;

}

/**
* @param time斜杠分开
* @return
*/
public static String timeslashData(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy/MM/dd");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
// int i = Integer.parseInt(time);
String times = sdr.format(new Date(lcc * 1000L));
return times;

}

/**
* @param time斜杠分开
* @return
*/
public static String timeMinute(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("HH:mm");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;

}

public static String tim(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyyMMdd HH:mm");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;
}

public static String time(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;
}

// 调用此方法输入所要转换的时间戳例如(1402733340)输出("2014年06月14日16时09分00秒")
public static String times(long timeStamp) {
SimpleDateFormat sdr = new SimpleDateFormat("MM月dd日 # HH:mm");
return sdr.format(new Date(timeStamp)).replaceAll("#",
getWeek(timeStamp));

}

private static String getWeek(long timeStamp) {
int mydate = 0;
String week = null;
Calendar cd = Calendar.getInstance();
cd.setTime(new Date(timeStamp));
mydate = cd.get(Calendar.DAY_OF_WEEK);
// 获取指定日期转换成星期几
if (mydate == 1) {
week = "周日";
} else if (mydate == 2) {
week = "周一";
} else if (mydate == 3) {
week = "周二";
} else if (mydate == 4) {
week = "周三";
} else if (mydate == 5) {
week = "周四";
} else if (mydate == 6) {
week = "周五";
} else if (mydate == 7) {
week = "周六";
}
return week;
}

// 并用分割符把时间分成时间数组
/**
* 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014-06-14-16-09-00")
*
* @param time
* @return
*/
public String timesOne(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;

}

public static String timesTwo(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;

}

/**
* 并用分割符把时间分成时间数组
*
* @param time
* @return
*/
public static String[] timestamp(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
String[] fenge = times.split("[年月日时分秒]");
return fenge;
}

/**
* 根据传递的类型格式化时间
*
* @param str
* @param type
* 例如:yy-MM-dd
* @return
*/
public static String getDateTimeByMillisecond(String str, String type) {

Date date = new Date(Long.valueOf(str));

SimpleDateFormat format = new SimpleDateFormat(type);

String time = format.format(date);

return time;
}

/**
* 分割符把时间分成时间数组
*
* @param time
* @return
*/
public String[] division(String time) {

String[] fenge = time.split("[年月日时分秒]");

return fenge;

}

/**
* 输入时间戳变星期
*
* @param time
* @return
*/
public static String changeweek(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
Date date = null;
int mydate = 0;
String week = null;
try {
date = sdr.parse(times);
Calendar cd = Calendar.getInstance();
cd.setTime(date);
mydate = cd.get(Calendar.DAY_OF_WEEK);
// 获取指定日期转换成星期几
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (mydate == 1) {
week = "星期日";
} else if (mydate == 2) {
week = "星期一";
} else if (mydate == 3) {
week = "星期二";
} else if (mydate == 4) {
week = "星期三";
} else if (mydate == 5) {
week = "星期四";
} else if (mydate == 6) {
week = "星期五";
} else if (mydate == 7) {
week = "星期六";
}
return week;

}

/**
* 获取日期和星期 例如:2014-11-13 11:00 星期一
*
* @param time
* @param type
* @return
*/
public static String getDateAndWeek(String time, String type) {
return getDateTimeByMillisecond(time + "000", type) + " "
+ changeweekOne(time);
}

/**
* 输入时间戳变星期
*
* @param time
* @return
*/
public static String changeweekOne(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
Date date = null;
int mydate = 0;
String week = null;
try {
date = sdr.parse(times);
Calendar cd = Calendar.getInstance();
cd.setTime(date);
mydate = cd.get(Calendar.DAY_OF_WEEK);
// 获取指定日期转换成星期几
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (mydate == 1) {
week = "星期日";
} else if (mydate == 2) {
week = "星期一";
} else if (mydate == 3) {
week = "星期二";
} else if (mydate == 4) {
week = "星期三";
} else if (mydate == 5) {
week = "星期四";
} else if (mydate == 6) {
week = "星期五";
} else if (mydate == 7) {
week = "星期六";
}
return week;

}

/**
* 获取当前时间
*
* @return
*/
public static String getCurrentTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return sdf.format(new java.util.Date());
}

/**
* 输入日期如(2014年06月14日16时09分00秒)返回(星期数)
*
* @param time
* @return
*/
public String week(String time) {
Date date = null;
SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
int mydate = 0;
String week = null;
try {
date = sdr.parse(time);
Calendar cd = Calendar.getInstance();
cd.setTime(date);
mydate = cd.get(Calendar.DAY_OF_WEEK);
// 获取指定日期转换成星期几
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (mydate == 1) {
week = "星期日";
} else if (mydate == 2) {
week = "星期一";
} else if (mydate == 3) {
week = "星期二";
} else if (mydate == 4) {
week = "星期三";
} else if (mydate == 5) {
week = "星期四";
} else if (mydate == 6) {
week = "星期五";
} else if (mydate == 7) {
week = "星期六";
}
return week;
}

/**
* 输入日期如(2014-06-14-16-09-00)返回(星期数)
*
* @param time
* @return
*/
public String weekOne(String time) {
Date date = null;
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
int mydate = 0;
String week = null;
try {
date = sdr.parse(time);
Calendar cd = Calendar.getInstance();
cd.setTime(date);
mydate = cd.get(Calendar.DAY_OF_WEEK);
// 获取指定日期转换成星期几
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (mydate == 1) {
week = "星期日";
} else if (mydate == 2) {
week = "星期一";
} else if (mydate == 3) {
week = "星期二";
} else if (mydate == 4) {
week = "星期三";
} else if (mydate == 5) {
week = "星期四";
} else if (mydate == 6) {
week = "星期五";
} else if (mydate == 7) {
week = "星期六";
}
return week;
}
}
坚持原创技术分享,您的支持将鼓励我继续创作!