JAVA常用日期工具类

本文实例为大家分享了Java工具类DateUtils的具体代码,供大家参考,具体内容如下

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
import  java.text.ParseException;
import  java.text.SimpleDateFormat;
import  java.util.Calendar;
import  java.util.Date;
/**
  * 描述:公共日期工具类
  */
public  class  DateUtils {
 
   public  static  String DATE_FORMAT = "yyyy-MM-dd" ;
 
   public  static  String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss" ;
 
   public  static  String DATE_FORMAT_CHINESE = "yyyy年M月d日" ;
 
   /**
    * 获取当前日期
    *
    *
    * @return
    *
    */
   public  static  String getCurrentDate() {
     String datestr = null ;
     SimpleDateFormat df = new  SimpleDateFormat(DateUtils.DATE_FORMAT);
     datestr = df.format( new  Date());
     return  datestr;
   }
 
   /**
    * 获取当前日期时间
    *
    *
    * @return
    *
    */
   public  static  String getCurrentDateTime() {
     String datestr = null ;
     SimpleDateFormat df = new  SimpleDateFormat(DateUtils.DATE_TIME_FORMAT);
     datestr = df.format( new  Date());
     return  datestr;
   }
 
   /**
    * 获取当前日期时间
    *
    *
    * @return
    *
    */
   public  static  String getCurrentDateTime(String Dateformat) {
     String datestr = null ;
     SimpleDateFormat df = new  SimpleDateFormat(Dateformat);
     datestr = df.format( new  Date());
     return  datestr;
   }
 
   public  static  String dateToDateTime(Date date) {
     String datestr = null ;
     SimpleDateFormat df = new  SimpleDateFormat(DateUtils.DATE_TIME_FORMAT);
     datestr = df.format(date);
     return  datestr;
   }
   /**
    * 将字符串日期转换为日期格式
    *
    *
    * @param datestr
    * @return
    *
    */
   public  static  Date stringToDate(String datestr) {
 
       if (datestr == null  ||datestr.equals( "" )){
         return  null ;
       }
     Date date = new  Date();
     SimpleDateFormat df = new  SimpleDateFormat(DateUtils.DATE_FORMAT);
     try  {
       date = df.parse(datestr);
     } catch  (ParseException e) {
       date=DateUtils.stringToDate(datestr, "yyyyMMdd" );
     }
     return  date;
   }
 
   /**
    * 将字符串日期转换为日期格式
    * 自定義格式
    *
    * @param datestr
    * @return
    *
    */
   public  static  Date stringToDate(String datestr, String dateformat) {
     Date date = new  Date();
     SimpleDateFormat df = new  SimpleDateFormat(dateformat);
     try  {
       date = df.parse(datestr);
     } catch  (ParseException e) {
       e.printStackTrace();
     }
     return  date;
   }
 
 
 
 
   /**
    * 将日期格式日期转换为字符串格式
    *
    *
    * @param date
    * @return
    *
    */
   public  static  String dateToString(Date date) {
     String datestr = null ;
     SimpleDateFormat df = new  SimpleDateFormat(DateUtils.DATE_FORMAT);
     datestr = df.format(date);
     return  datestr;
   }
 
   /**
    * 将日期格式日期转换为字符串格式 自定義格式
    *
    * @param date
    * @param dateformat
    * @return
    */
   public  static  String dateToString(Date date, String dateformat) {
     String datestr = null ;
     SimpleDateFormat df = new  SimpleDateFormat(dateformat);
     datestr = df.format(date);
     return  datestr;
   }
 
   /**
    * 获取日期的DAY值
    *
    *
    * @param date
    *      输入日期
    * @return
    *
    */
   public  static  int  getDayOfDate(Date date) {
     int  d = 0 ;
     Calendar cd = Calendar.getInstance();
     cd.setTime(date);
     d = cd.get(Calendar.DAY_OF_MONTH);
     return  d;
   }
 
   /**
    * 获取日期的MONTH值
    *
    *
    * @param date
    *      输入日期
    * @return
    *
    */
   public  static  int  getMonthOfDate(Date date) {
     int  m = 0 ;
     Calendar cd = Calendar.getInstance();
     cd.setTime(date);
     m = cd.get(Calendar.MONTH) + 1 ;
     return  m;
   }
 
   /**
    * 获取日期的YEAR值
    *
    *
    * @param date
    *      输入日期
    * @return
    *
    */
   public  static  int  getYearOfDate(Date date) {
     int  y = 0 ;
     Calendar cd = Calendar.getInstance();
     cd.setTime(date);
     y = cd.get(Calendar.YEAR);
     return  y;
   }
 
   /**
    * 获取星期几
    *
    *
    * @param date
    *      输入日期
    * @return
    *
    */
   public  static  int  getWeekOfDate(Date date) {
     int  wd = 0 ;
     Calendar cd = Calendar.getInstance();
     cd.setTime(date);
     wd = cd.get(Calendar.DAY_OF_WEEK) - 1 ;
     return  wd;
   }
 
   /**
    * 获取输入日期的当月第一天
    *
    *
    * @param date
    *      输入日期
    * @return
    *
    */
   public  static  Date getFirstDayOfMonth(Date date) {
     Calendar cd = Calendar.getInstance();
     cd.setTime(date);
     cd.set(Calendar.DAY_OF_MONTH, 1 );
     return  cd.getTime();
   }
 
   /**
    * 获得输入日期的当月最后一天
    *
    * @param date
    */
   public  static  Date getLastDayOfMonth(Date date) {
     return  DateUtils.addDay(DateUtils.getFirstDayOfMonth(DateUtils.addMonth(date, 1 )), - 1 );
   }
 
   /**
    * 判断是否是闰年
    *
    *
    * @param date
    *      输入日期
    * @return 是true 否false
    *
    */
   public  static  boolean  isLeapYEAR(Date date) {
 
     Calendar cd = Calendar.getInstance();
     cd.setTime(date);
     int  year = cd.get(Calendar.YEAR);
 
     if  (year % 4  == 0  && year % 100  != 0  | year % 400  == 0 ) {
       return  true ;
     } else  {
       return  false ;
     }
   }
 
   /**
    * 根据整型数表示的年月日,生成日期类型格式
    *
    * @param year
    *      年
    * @param month
    *      月
    * @param day
    *      日
    * @return
    *
    */
   public  static  Date getDateByYMD( int  year, int  month, int  day) {
     Calendar cd = Calendar.getInstance();
     cd.set(year, month- 1 , day);
     return  cd.getTime();
   }
 
   /**
    * 获取年周期对应日
    *
    * @param date
    *      输入日期
    * @param iyear
    *      年数  負數表示之前
    * @return
    *
    */
   public  static  Date getYearCycleOfDate(Date date, int  iyear) {
     Calendar cd = Calendar.getInstance();
     cd.setTime(date);
 
     cd.add(Calendar.YEAR, iyear);
 
     return  cd.getTime();
   }
 
   /**
    * 获取月周期对应日
    *
    * @param date
    *      输入日期
    * @param i
    * @return
    *
    */
   public  static  Date getMonthCycleOfDate(Date date, int  i) {
     Calendar cd = Calendar.getInstance();
     cd.setTime(date);
 
     cd.add(Calendar.MONTH, i);
 
     return  cd.getTime();
   }
 
   /**
    * 计算 fromDate 到 toDate 相差多少年
    *
    * @param fromDate
    * @param toDate
    * @return 年数
    *
    */
   public  static  int  getYearByMinusDate(Date fromDate, Date toDate) {
     Calendar df=Calendar.getInstance();
     df.setTime(fromDate);
 
     Calendar dt=Calendar.getInstance();
     dt.setTime(toDate);
 
     return  dt.get(Calendar.YEAR)-df.get(Calendar.YEAR);
   }
 
   /**
    * 计算 fromDate 到 toDate 相差多少个月
    *
    * @param fromDate
    * @param toDate
    * @return 月数
    *
    */
   public  static  int  getMonthByMinusDate(Date fromDate, Date toDate) {
     Calendar df=Calendar.getInstance();
     df.setTime(fromDate);
 
     Calendar dt=Calendar.getInstance();
     dt.setTime(toDate);
 
     return  dt.get(Calendar.YEAR)* 12 +dt.get(Calendar.MONTH)-
         (df.get(Calendar.YEAR)* 12 +df.get(Calendar.MONTH));
   }
 
   /**
    * 计算 fromDate 到 toDate 相差多少天
    *
    * @param fromDate
    * @param toDate
    * @return 天数
    *
    */
   public  static  long  getDayByMinusDate(Object fromDate, Object toDate) {
 
     Date f=DateUtils.chgObject(fromDate);
 
     Date t=DateUtils.chgObject(toDate);
 
     long  fd=f.getTime();
     long  td=t.getTime();
 
     return  (td-fd)/(24L * 60L * 60L * 1000L);
   }
 
   /**
    * 计算年龄
    *
    * @param birthday
    *      生日日期
    * @param calcDate
    *      要计算的日期点
    * @return
    *
    */
   public  static  int  calcAge(Date birthday, Date calcDate) {
 
     int  cYear=DateUtils.getYearOfDate(calcDate);
     int  cMonth=DateUtils.getMonthOfDate(calcDate);
     int  cDay=DateUtils.getDayOfDate(calcDate);  
     int  bYear=DateUtils.getYearOfDate(birthday);
     int  bMonth=DateUtils.getMonthOfDate(birthday);
     int  bDay=DateUtils.getDayOfDate(birthday);
 
     if (cMonth>bMonth||(cMonth==bMonth&&cDay>bDay)){
       return  cYear-bYear;
     } else {
       return  cYear- 1 -bYear;
     }
   }
 
   /**
    * 从身份证中获取出生日期
    *
    * @param idno
    *      身份证号码
    * @return
    *
    */
   public  static  String getBirthDayFromIDCard(String idno) {
     Calendar cd = Calendar.getInstance();
     if  (idno.length() == 15 ) {
       cd.set(Calendar.YEAR, Integer.valueOf( "19"  + idno.substring( 6 , 8 ))
           .intValue());
       cd.set(Calendar.MONTH, Integer.valueOf(idno.substring( 8 , 10 ))
           .intValue() - 1 );
       cd.set(Calendar.DAY_OF_MONTH,
           Integer.valueOf(idno.substring( 10 , 12 )).intValue());
     } else  if  (idno.length() == 18 ) {
       cd.set(Calendar.YEAR, Integer.valueOf(idno.substring( 6 , 10 ))
           .intValue());
       cd.set(Calendar.MONTH, Integer.valueOf(idno.substring( 10 , 12 ))
           .intValue() - 1 );
       cd.set(Calendar.DAY_OF_MONTH,
           Integer.valueOf(idno.substring( 12 , 14 )).intValue());
     }
     return  DateUtils.dateToString(cd.getTime());
   }
 
   /**
    * 在输入日期上增加(+)或减去(-)天数
    *
    * @param date
    *      输入日期
    * @param imonth
    *      要增加或减少的天数
    */
   public  static  Date addDay(Date date, int  iday) {
     Calendar cd = Calendar.getInstance();
 
     cd.setTime(date);
 
     cd.add(Calendar.DAY_OF_MONTH, iday);
 
     return  cd.getTime();
   }
 
   /**
    * 在输入日期上增加(+)或减去(-)月份
    *
    * @param date
    *      输入日期
    * @param imonth
    *      要增加或减少的月分数
    */
   public  static  Date addMonth(Date date, int  imonth) {
     Calendar cd = Calendar.getInstance();
 
     cd.setTime(date);
 
     cd.add(Calendar.MONTH, imonth);
 
     return  cd.getTime();
   }
 
   /**
    * 在输入日期上增加(+)或减去(-)年份
    *
    * @param date
    *      输入日期
    * @param imonth
    *      要增加或减少的年数
    */
   public  static  Date addYear(Date date, int  iyear) {
     Calendar cd = Calendar.getInstance();
 
     cd.setTime(date);
 
     cd.add(Calendar.YEAR, iyear);
 
     return  cd.getTime();
   }
 
   /**
    * 將OBJECT類型轉換為Date
    * @param date
    * @return
    */
   public  static  Date chgObject(Object date){
 
     if (date!= null &&date instanceof  Date){
       return  (Date)date;
     }
 
     if (date!= null &&date instanceof  String){
       return  DateUtils.stringToDate((String)date);
     }
 
     return  null ;
 
   }
 
   public  static  long  getAgeByBirthday(String date){
 
     Date birthday = stringToDate(date, "yyyy-MM-dd" );
     long  sec = new  Date().getTime() - birthday.getTime();
 
     long  age = sec/( 1000 * 60 * 60 * 24 )/ 365 ;
 
     return  age;
   }
 
 
   /**
    * @param args
    */
   public  static  void  main(String[] args) {
     //String temp = DateUtil.dateToString(getLastDayOfMonth(new Date()),
     ///   DateUtil.DATE_FORMAT_CHINESE);
     //String s=DateUtil.dateToString(DateUtil.addDay(DateUtil.addYear(new Date(),1),-1));
 
 
     long  s=DateUtils.getDayByMinusDate( "2012-01-01" , "2012-12-31" );
     System.err.println(s);
 
 
   }
 

猜你喜欢

转载自www.cnblogs.com/xifenglou/p/9103498.html