本文实例为大家分享了时间转换的DateUtils工具类,供大家参考,具体内容如下
java" id="highlighter_3872">
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* 时间日期工具类
*
* @author wul
* 2015-12-31
*/
public class DateUtil {
public static final String DATE_NORMAL_FORMAT = "yyyy-MM-dd" ;
public static final String DATETIME_NORMAL_FORMAT = "yyyy-MM-dd HH:mm:ss" ;
public static final String DATE_COMPACT_FORMAT = "yyyyMMdd" ;
public static final String DATETIME_COMPACT_FORMAT = "yyyyMMddHHmmss" ;
public static final String YM_NORMAL_FORMAT = "yyyy-MM" ;
public static final String YM_COMPACT_FORMAT = "yyyyMM" ;
/**
* String转Timestamp
* @param dateStr
* @return
* @author wul
* 2016-1-17
*/
public static Timestamp stringToTimestamp(String dateStr) {
try {
if (dateStr.length() <= 10 ) {
dateStr += " 00:00:00" ;
}
return Timestamp.valueOf(dateStr);
} catch (Exception e) {
e.printStackTrace();
return null ;
}
}
/**
* String转Date
* @param dateStr
* @param format
* @return
* @author wul
* 2016-1-17
*/
public static Date stringToDate(String dateStr, String format) {
if (dateStr == null || "" .equals(dateStr)){
return null ;
}
Date date = null ;
//注意format的格式要与日期String的格式相匹配
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
date = sdf.parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* Date转String
* @param date
* @param format
* @return
* @author wul
* 2016-1-17
*/
public static String dateToString(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
String currentDate = sdf.format(date);
return currentDate;
}
/**
* Date转Timestamp
* @param date
* @return
* @author wul
* 2016-1-17
*/
public static Timestamp dateToTimestamp(Date date) {
Timestamp ts = new Timestamp(date.getTime());
return ts;
}
/**
* Timestamp转String
* @param ts
* @return
* @author wul
* 2016-1-17
*/
public static String timestampToString(Timestamp ts) {
String tsStr = null ;
SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_NORMAL_FORMAT);
try {
tsStr = sdf.format(ts);
} catch (Exception e) {
e.printStackTrace();
}
return tsStr;
}
/**
* Timestamp转Date
* @param ts
* @return
* @author wul
* 2016-1-17
*/
public static Date timestampToDate(Timestamp ts) {
return ts;
}
/**
* 获得当前时间并格式化:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String getCurrentTimeNormal() {
SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_NORMAL_FORMAT);
String currentDate = sdf.format( new Date());
return currentDate;
}
/**
* 获得当前时间并格式化:yyyyMMddHHmmss
* @return
*/
public static String getCurrentTimeCompact() {
SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_COMPACT_FORMAT);
String currentDate = sdf.format( new Date());
return currentDate;
}
/**
* 获得当前时间并格式化:yyyy-MM-dd
* @return
*/
public static String getCurrentDateNormal() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_NORMAL_FORMAT);
String currentDate = sdf.format( new Date());
return currentDate;
}
/**
* 获得当前时间并格式化:yyyyMMdd
* @return
*/
public static String getCurrentDateCompact() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_COMPACT_FORMAT);
String currentDate = sdf.format( new Date());
return currentDate;
}
/**
* 将20101202时间格式化为2010-12-02
*
* @param DateString 时间格式:yyyyMMdd
* @return
*/
public static String getDateCompactToNormal(String DateString){
StringBuilder sb = new StringBuilder();
sb.append(DateString.substring( 0 , 4 )).append( "-" ).append(DateString.subSequence( 4 , 6 )).append( "-" ).append(DateString.substring( 6 , 8 ));
return sb.toString();
}
/**
* 将20101202101423时间格式化为2010-12-02 10:14:23
*
* @param DateString 时间格式:yyyyMMddHHmmss
* @return
*/
public static String getDateTimeCompactToNormal(String DateString){
StringBuilder sb = new StringBuilder();
sb.append(DateString.substring( 0 , 4 )).append( "-" ).append(DateString.subSequence( 4 , 6 )).append( "-" ).append(DateString.substring( 6 , 8 ))
.append( " " ).append(DateString.substring( 8 , 10 )).append( ":" ).append(DateString.substring( 10 , 12 )).append( ":" ).append(DateString.substring( 12 ));
return sb.toString();
}
/**
* 把界面输入的时间转为间凑的时间字符串
* 将2010-12-02 10:14:23时间格式化为20101202101423
* @param dateNormalStr String
* @return String
*/
public static String getCompactString(String dateNormalStr) {
StringBuffer ret = new StringBuffer();
try {
ret.append(dateNormalStr.substring( 0 , 4 ));
ret.append(dateNormalStr.substring( 5 , 7 ));
ret.append(dateNormalStr.substring( 8 , 10 ));
ret.append(dateNormalStr.substring( 11 , 13 ));
ret.append(dateNormalStr.substring( 14 , 16 ));
ret.append(dateNormalStr.substring( 17 , 19 ));
} catch (Exception ex) {
// 如果字串不够长度,则返回前面的部分
}
return ret.toString();
}
/**
* 将20101202(101423)时间格式 获得年份
* @param DateString 时间格式:yyyyMMdd(HHmmss)
* @return
*/
public static String getYear(String DateString){
return DateString.substring( 0 , 4 );
}
/**
* 将20101202(101423)时间格式 获得月份
* @param DateString 时间格式:yyyyMMdd(HHmmss)
* @return
*/
public static String getMonth(String DateString){
return DateString.substring( 4 , 6 );
}
/**
* 将20101202时间格式 获得日期
* @param DateString 时间格式:yyyyMMdd
* @return
*/
public static String getDayNoTime(String DateString){
return DateString.substring( 6 );
}
/**
* 获取当前日期之前的日期,按天数向前推
* @param numVal
* @param dateFormat
* @return
* @author wul
* 2016-1-17
*/
public static String getBeforeDatePlusDay( int numVal, String dateFormat) {
Calendar calendar = Calendar.getInstance();
long currentTimeMillis = calendar.getTimeInMillis();
long hourInMillis = 60 * 60 * 1000 ;
long dVal = numVal * 24 * hourInMillis;
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String currentDate = sdf.format(currentTimeMillis - dVal);
return currentDate;
}
/**
* 获取当前日期之前的日期,按天数向前推
* @param numVal
* @param dateFormat
* @return
* @author wul
* 2016-1-17
*/
public static String getAfterDatePlusDay( int numVal, String dateFormat) {
Calendar calendar = Calendar.getInstance();
long currentTimeMillis = calendar.getTimeInMillis();
long hourInMillis = 60 * 60 * 1000 ;
long dVal = numVal * 24 * hourInMillis;
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String currentDate = sdf.format(currentTimeMillis + dVal);
return currentDate;
}
/**
* 获取当前日期之前的日期,按小时向前推
* @param numVal
* @param dateFormat
* @return
* @author wul
* 2016-1-17
*/
public static String getBeforeDatePlusHour( int numVal, String dateFormat) {
Calendar calendar = Calendar.getInstance();
long currentTimeMillis = calendar.getTimeInMillis();
long hourInMillis = 60 * 60 * 1000 ;
long dVal = numVal * hourInMillis;
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String currentDate = sdf.format(currentTimeMillis - dVal);
return currentDate;
}
/**
* 获取当前日期之前的日期,按小时向前推
* @param numVal
* @param dateFormat
* @return
* @author wul
* 2016-1-17
*/
public static String getAfterDatePlusHour( int numVal, String dateFormat) {
Calendar calendar = Calendar.getInstance();
long currentTimeMillis = calendar.getTimeInMillis();
long hourInMillis = 60 * 60 * 1000 ;
long dVal = numVal * hourInMillis;
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String currentDate = sdf.format(currentTimeMillis + dVal);
return currentDate;
}
/**
* 两个日期相差天数
* @param beginDate
* @param endDate
* @return
* @author wul
* 2016-1-18
*/
public static int daysBetween(Date beginDate, Date endDate) {
Calendar cal = Calendar.getInstance();
cal.setTime(beginDate);
long time1 = cal.getTimeInMillis();
cal.setTime(endDate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / ( 1000 * 3600 * 24 );
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 获取某月天数
* @param year
* @param month
* @return
* @author wul
* 2016-1-18
*/
public static int getMonthdays( int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1 );
return cal.getActualMaximum(Calendar.DATE);
}
/**
* 给时间加减年份
* @param date
* @param plusTime
* @return
* @author wul
* 2016-1-18
*/
public static Date getDatePlusYear(Date date, int plusTime) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, plusTime);
Date d = cal.getTime();
return d;
}
/**
* 给时间加减月份
* @param date
* @param plusTime
* @return
* @author wul
* 2016-1-18
*/
public static Date getDatePlusMonth(Date date, int plusTime) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, plusTime);
Date d = cal.getTime();
return d;
}
/**
* 给时间加减天数
* @param date
* @param plusTime
* @return
* @author wul
* 2016-1-18
*/
public static Date getDatePlusDay(Date date, int plusTime) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, plusTime);
Date d = cal.getTime();
return d;
}
/**
* 给时间加减小时
* @param date
* @param plusTime
* @return
* @author wul
* 2016-1-18
*/
public static Date getDatePlusHour(Date date, int plusTime) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, plusTime);
Date d = cal.getTime();
return d;
}
/**
* 给时间加减分钟
* @param date
* @param plusTime
* @return
* @author wul
* 2016-1-18
*/
public static Date getDatePlusMinute(Date date, int plusTime) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, plusTime);
Date d = cal.getTime();
return d;
}
/**
* 给时间加减秒
* @param date
* @param plusTime
* @return
* @author wul
* 2016-1-18
*/
public static Date getDatePlusSecond(Date date, int plusTime) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.SECOND, plusTime);
Date d = cal.getTime();
return d;
}
/**
* 返回当前年
* @return
* @author wul
* 2016-1-18
*/
public static int getCurrentYear() {
Calendar calendar = Calendar.getInstance();
return calendar.get( 1 );
}
/**
* 返回当前月
* @return
* @author wul
* 2016-1-18
*/
public static int getCurrentMonth() {
Calendar calendar = Calendar.getInstance();
return calendar.get( 2 ) + 1 ;
}
/**
* 返回当前天
* @return
* @author wul
* 2016-1-18
*/
public static int getCurrentDay() {
Calendar calendar = Calendar.getInstance();
return calendar.get( 5 );
}
/**
* 返回当前小时
* @return
* @author wul
* 2016-1-18
*/
public static int getCurrentHour() {
Calendar calendar = Calendar.getInstance();
return calendar.get( 11 );
}
/**
* 返回当前分钟
* @return
* @author wul
* 2016-1-18
*/
public static int getCurrentMinute() {
Calendar calendar = Calendar.getInstance();
return calendar.get( 12 );
}
/**
* 返回当前秒
* @return
* @author wul
* 2016-1-18
*/
public static int getCurrentSecond() {
Calendar calendar = Calendar.getInstance();
return calendar.get( 13 );
}
/**
* 返回当前年
* @return
* @author wul
* 2016-1-18
*/
public static int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get( 1 );
}
/**
* 返回当前月
* @return
* @author wul
* 2016-1-18
*/
public static int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get( 2 ) + 1 ;
}
/**
* 返回当前天
* @return
* @author wul
* 2016-1-18
*/
public static int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get( 5 );
}
/**
* 返回当前小时
* @return
* @author wul
* 2016-1-18
*/
public static int getHour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get( 11 );
}
/**
* 返回当前分钟
* @return
* @author wul
* 2016-1-18
*/
public static int getMinute(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get( 12 );
}
/**
* 返回当前秒
* @return
* @author wul
* 2016-1-18
*/
public static int getSecond(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get( 13 );
}
public static void main(String[] args) {
System.out.println(DateUtil.dateToString( new java.sql.Date(System.currentTimeMillis()), DateUtil.DATE_NORMAL_FORMAT));
Map<String,Object> map = new HashMap<String,Object>();
map.put( "date" , new Date());
String json = JSONObject.fromObject(map).toString();
System.out.println(json);
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/Makenzie/article/details/54426707