一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java日期操作方法工具類實例【包含日期比較大小,相加減,判斷,驗證,獲取年份等】

Java日期操作方法工具類實例【包含日期比較大小,相加減,判斷,驗證,獲取年份等】

2021-01-30 11:56LovooGod Java教程

這篇文章主要介紹了Java日期操作方法工具類,結合完整實例形式分析了java針對日期的各種常見操作,包括日期比較大小,相加減,判斷,驗證,獲取年份、天數、星期等,需要的朋友可以參考下

本文實例講述了Java日期操作方法工具類。分享給大家供大家參考,具體如下:

?
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
package com.gcloud.common;
import org.apache.http.util.TextUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;
/**
 * 日期時間工具類
 * Created by charlin on 2017/9/3.
 */
public class DateUtil {
 public static final String CHINA_DATE_FORMAT = "yyyy年MM月dd日";
 public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
 public static final String DATE_FORMAT = "yyyy-MM-dd";
 public static final String TIME_FORMAT = "HH:mm:ss";
 //----------------判斷-----------------------------------------------
 /**
 * 是否是潤年
 * @param yearNum
 * @return
 */
 public static boolean isLeapYear(int yearNum) {
 boolean isLeep = false;
 if ((yearNum % 4 == 0) && (yearNum % 100 != 0))
  isLeep = true;
 else if (yearNum % 400 == 0)
  isLeep = true;
 else {
  isLeep = false;
 }
 return isLeep;
 }
 /**
 * 判斷是否是日期
 *
 * @param date
 * @return
 */
 public static boolean isDate(String date) {
 //判斷年月日的正則表達式,接受輸入格式為2010-12-24,可接受平年閏年的日期
 String regex = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
 Pattern pattern = Pattern.compile(regex);
 return pattern.matcher(date).matches();
 }
 /**
 * 驗證是不是生日
 *
 * @param birthday
 * @return
 */
 public static boolean verifyBirthDay(String birthday) {
 if (TextUtils.isEmpty(birthday)) return false;
 if (!birthday.contains("-")) return false;
 String[] arr = birthday.split("-");
 if (null == arr || arr.length != 3 || arr[0].length() != 4 || arr[1].length() != 2 || arr[2].length() != 2)
  return false;
 int year = getYear(new Date());
 int birthYear = Integer.parseInt(arr[0]);
 if (birthYear <= 1900 || birthYear > year) return false;
 String curDate = formatDate(new Date(), DATE_FORMAT);
 if (birthday.compareTo(curDate) > 0) return false;
 return isDate(birthday);
 }
 //-------------------自動轉化--------------------------------------------
 /**
 * 把字符串自動轉化為時間格式
 *
 * @param dateStr
 * @return
 */
 public static Date parseDateByAuto(String dateStr) {
 if (StringUtil.isEmpty(dateStr)) {
  return null;
 }
 String format = DATE_FORMAT;
 if (dateStr.indexOf("/") > -1) {
  format = format.replace("-", "/");
 }
 if (dateStr.indexOf(":") != -1) {
  format += " HH:mm";
 }
 //存在秒
 if (dateStr.indexOf(":") != dateStr.lastIndexOf(":")) {
  format += ":ss";
 }
 return parseDate(dateStr, format);
 }
 /**
 * 自動識別格式
 * @param date
 * @return
 */
 public static String formatDateByAuto(Date date){
 String format = DATE_FORMAT;
 if( !(date instanceof java.sql.Date) && (date.getSeconds()>0||date.getMinutes()>0||date.getHours()>0)){
  format = DATETIME_FORMAT;
 }
 return formatDate(date, format);
 }
 //------------當前日期與時間 --------------------------------
 /**取當前日期*/
 public static Date getCurrDate() {return parseDate(formatDate(new Date())); }
 /**取當前時間*/
 public static Date getCurrDateTime() {return parseDate(formatDate(new Date())); }
 /**取當前日期*/
 public static String getCurrDateStr() { return formatDate(new Date()); }
 /**取當前時間*/
 public static String getCurrDateTimeStr() { return formatDate(new Date(), DATETIME_FORMAT);}
 public static String formatCurrDate() {return formatDate(new Date(),DATE_FORMAT); }
 public static String formatCurrDateTime() {return formatDate(new Date(),DATETIME_FORMAT);}
 public static String formatCurrDateToS(String strFormat) {return formatDate(new Date(), strFormat); }
 //-----------時間計算--------------------------------------------
 /**
 * 時間相減
 * @param strDateBegin
 * @param strDateEnd
 * @param iType
 * @return
 */
 public static int getDiffDate(String strDateBegin, String strDateEnd, int iType) {
 Calendar calBegin = Calendar.getInstance();
 calBegin.setTime(parseDate(strDateBegin, DATETIME_FORMAT));
 Calendar calEnd = Calendar.getInstance();
 calBegin.setTime(parseDate(strDateEnd, DATETIME_FORMAT));
 long lBegin = calBegin.getTimeInMillis();
 long lEnd = calEnd.getTimeInMillis();
 if (iType == Calendar.SECOND)
  return (int) ((lEnd - lBegin) / 1000L);
 if (iType == Calendar.MINUTE)
  return (int) ((lEnd - lBegin) / 60000L);
 if (iType == Calendar.HOUR)
  return (int) ((lEnd - lBegin) / 3600000L);
 if (iType == Calendar.DAY_OF_MONTH) {
  return (int) ((lEnd - lBegin) / 86400000L);
 }
 return -1;
 }
 /**
 * 添加天數或月份或年得到新的時間
 *
 * @param strDate
 * @param count
 * @param dayType Calendar.YEAR
 * @return
 */
 public static String getAddDateTime(String strDate, int count, int dayType) {
 Calendar cal = Calendar.getInstance();
 cal.setTime(parseDate(strDate));
 cal.add(dayType, count);
 SimpleDateFormat sdf = null;
 if ((dayType == Calendar.YEAR) || (dayType == Calendar.MONTH) || (dayType == Calendar.DAY_OF_MONTH))
  sdf = new SimpleDateFormat(DATE_FORMAT);
 else
  sdf = new SimpleDateFormat(DATETIME_FORMAT);
 return sdf.format(cal.getTime());
 }
 /**
 * 日期增加天數
 * @param date
 * @param iCount
 * @return
 */
 public static Date getAddDate(Date date, int iCount) {
 Calendar cal = Calendar.getInstance();
 cal.setTime(date);
 cal.add(Calendar.DAY_OF_MONTH, iCount);
 return cal.getTime();
 }
 /**
 * 比較日期
 *
 * @param dateStr1
 * @param dateStr2
 * @return
 */
 public static int compareDate(String dateStr1, String dateStr2) {
 Date date1 = parseDate(dateStr1);
 Date date2 = parseDate(dateStr2);
 if (date1.getTime() > date2.getTime())
  return -1;
 else if (date1.getTime() < date2.getTime())
  return 1;
 else
  return 0;
 }
 public static int compareDate(Date date1, Date date2) {
 if (date1.getTime() > date2.getTime())
  return -1;
 else if (date1.getTime() < date2.getTime())
  return 1;
 else
  return 0;
 }
 /**
 * 時間差
 *
 * @param startDate
 * @param endDate
 * @return
 */
 public static int getDiffDays(Date startDate, Date endDate) {
 int days = 0;
 if (startDate.after(endDate)) {
  Date temp = startDate;
  startDate = endDate;
  endDate = temp;
 }
 days = (int) (endDate.getTime() - startDate.getTime()) / 1000 * 60 * 60 * 24;
 return days;
 }
 /**
 * 當前日期的后幾天
 *
 * @param date
 * @param n
 * @return
 */
 public static Date getAfterDay(Date date, int n) {
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 c.add(Calendar.DATE, n);
 return c.getTime();
 }
 //---------獲取時間天數----------------------------------------------
 /**
 * 獲取當前月的最后一天
 *
 * @param dateStr
 * @return
 */
 public static String getMonthEnd(String dateStr) {
 //當前第一天
 Date date = parseDate(getMonthBegin(dateStr));
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 c.add(Calendar.MONTH, 1);
 c.add(Calendar.DAY_OF_YEAR, -1);
 return formatDate(c.getTime());
 }
 public static String getMonthEnd(Date date) {
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
 return formatDate(c.getTime());
 }
 /**
 * 獲得當前日期的月份第一天
 *
 * @param dateStr
 * @return
 */
 public static String getMonthBegin(String dateStr) {
 Date date = parseDate(dateStr);
 return formatDate(date, "yyyy-MM") + 01;
 }
 public static String getMonthBegin(Date date) {
 return formatDate(date, "yyyy-MM") + 01;
 }
 //--------------格式化日期-----------------------------------------
 /**
 * 格式化日期為字符串
 *
 * @param date
 * @param format
 * @return
 */
 public static String formatDate(Date date, String format) {
 return new SimpleDateFormat(format).format(date);
 }
 public static String formatDate(Date date) {
 return formatDate(date, DATE_FORMAT);
 }
 public static String formateChinaDate(Date date) {
 return formatDate(date, CHINA_DATE_FORMAT);
 }
 public static String formateDateTime(Date date) {
 return formatDate(date, DATETIME_FORMAT);
 }
 public static String formateTime(Date date) {
 return formatDate(date, TIME_FORMAT);
 }
 //-----------------格式化字符串為日期--------------------------------------
 /**
 * 格式化字符串為日期
 *
 * @param date
 * @param format
 * @return
 */
 public static Date parseDate(String date, String format) {
 try {
  return new SimpleDateFormat(format).parse(date);
 } catch (ParseException e) {
  e.printStackTrace();
 }
 return null;
 }
 public static Date parseDate(String date) { return parseDate(date, DATE_FORMAT);}
 public static Date parseChinaDate(String date) {
 return parseDate(date, CHINA_DATE_FORMAT);
 }
 public static Date parseDateTime(String date) {
 return parseDate(date, DATETIME_FORMAT);
 }
 public static Date parseTime(String date) {
 return parseDate(date, TIME_FORMAT);
 }
 //---獲取年月日時分秒----------------------------------------------------
 /**
 * 獲取年份
 *
 * @param date
 * @return
 */
 public static int getYear(Date date) {
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 return c.get(Calendar.YEAR);
 }
 /**
 * 獲取月份
 *
 * @param date
 * @return
 */
 public static int getMonth(Date date) {
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 return c.get(Calendar.MONTH) + 1;
 }
 /**
 * 獲取日
 *
 * @param date
 * @return
 */
 public static int getDay(Date date) {
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 return c.get(Calendar.DAY_OF_MONTH);
 }
 /**
 * 獲取星期
 *
 * @param date
 * @return
 */
 public static int getWeek(Date date) {
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 return c.get(Calendar.DAY_OF_WEEK);
 }
 /**
 * 獲取時間
 *
 * @param date
 * @return
 */
 public static int getHour(Date date) {
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 return c.get(Calendar.HOUR_OF_DAY);
 }
 /**
 * 獲取分種
 *
 * @param date
 * @return
 */
 public static int getMinute(Date date) {
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 return c.get(Calendar.MINUTE);
 }
 /**
 * 獲取秒
 *
 * @param date
 * @return
 */
 public static int getSecond(Date date) {
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 return c.get(Calendar.SECOND);
 }
 //--------------獲取星期幾---------------------------------------------------
 /**
 * 獲取星期幾
 *
 * @param strDate
 * @return
 */
 public static String getWeekDayName(String strDate) {
 String[] mName = {"日", "一", "二", "三", "四", "五", "六"};
 Date date = parseDate(strDate);
 int week = getWeek(date);
 return "星期" + mName[week];
 }
 public static String getWeekDayName(Date date) {
 String[] mName = {"日", "一", "二", "三", "四", "五", "六"};
 int week = getWeek(date);
 return "星期" + mName[week];
 }
 /**
 * 一年中的星期幾
 * @return
 */
 public static int getWeekNumOfYear(Date date) {
 Calendar calendar = Calendar.getInstance();
 calendar.setTime(date);
 return calendar.get(Calendar.WEEK_OF_YEAR);
 }
 public static int getWeekNumOfYear(String date) {
 Calendar calendar = Calendar.getInstance();
 calendar.setTime(parseDate(date, DATE_FORMAT));
 return calendar.get(Calendar.WEEK_OF_YEAR);
 }
 /**
 * 獲取本周星期一的日期
 * @param yearNum
 * @param weekNum
 * @return
 * @throws ParseException
 */
 public static String getYearWeekFirstDay(int yearNum, int weekNum) {
 Calendar cal = Calendar.getInstance();
 cal.set(Calendar.YEAR, yearNum);
 cal.set(Calendar.WEEK_OF_YEAR, weekNum);
 cal.set(Calendar.DAY_OF_WEEK, 2);
 String tempYear = Integer.toString(yearNum);
 String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
 String tempDay = Integer.toString(cal.get(Calendar.DAY_OF_MONTH)-1);
 return tempYear + "-" + tempMonth + "-" + tempDay;
 }
 /**
 * 獲取本周星期天的日期
 * @param yearNum
 * @param weekNum
 * @return
 * @throws ParseException
 */
 public static String getYearWeekEndDay(int yearNum, int weekNum) {
 Calendar cal = Calendar.getInstance();
 cal.set(Calendar.YEAR, yearNum);
 cal.set(Calendar.WEEK_OF_YEAR, weekNum + 1);
 cal.set(Calendar.DAY_OF_WEEK, 1);
 String tempYear = Integer.toString(yearNum);
 String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
 String tempDay = Integer.toString(cal.get(Calendar.DAY_OF_MONTH)-1);
 return tempYear + "-" + tempMonth + "-" + tempDay;
 }
 //--------------獲取天數---------------------------------------------------
 /**
 * 獲取某年某月的第一天
 * @param yearNum
 * @param monthNum
 * @return
 */
 public static Date getYearMonthFirstDay(int yearNum, int monthNum) {
 Calendar cal = Calendar.getInstance();
 cal.set(yearNum, monthNum - 1, 1, 0, 0, 0);
 cal.set(14, 0);
 return cal.getTime();
 }
 /**
 * 獲取某年下個月的第一天
 * @param yearNum
 * @param monthNum
 * @return
 */
 public static Date getNextYearMonthFirstDay(int yearNum, int monthNum) {
 Calendar cal = Calendar.getInstance();
 cal.set(yearNum, monthNum, 1, 0, 0, 0);
 cal.set(14, 0);
 return cal.getTime();
 }
 /**
 * 獲取某年某月的最后一天
 * @param yearNum
 * @param monthNum
 * @return
 */
 public static Date getYearMonthEndDay(int yearNum, int monthNum) {
 Calendar cal = Calendar.getInstance();
 cal.set(yearNum, monthNum, 0, 0, 0, 0);
 cal.set(14, 0);
 return cal.getTime();
 }
 /**
 * 獲取某月的第一天
 * @param date
 * @return
 */
 public static Date getYearMonthFirstDay(Date date) {
 Calendar cal = Calendar.getInstance();
 cal.setTime(date);
 cal.set(5, 1);
 cal.set(11, 0);
 cal.set(12, 0);
 cal.set(13, 0);
 cal.set(14, 0);
 return cal.getTime();
 }
 /**
 * 獲取下一年的第一天
 * @param date
 * @return
 */
 public static Date getNextYearMonthFirstDay(Date date) {
 Calendar cal = Calendar.getInstance();
 cal.setTime(date);
 cal.add(2, 1);
 cal.set(5, 1);
 cal.set(11, 0);
 cal.set(12, 0);
 cal.set(13, 0);
 cal.set(14, 0);
 return cal.getTime();
 }
 /**
 * 獲取當前月的最后一天
 * @param date
 * @return
 */
 public static Date getYearMonthEndDay(Date date) {
 Calendar cal = Calendar.getInstance();
 cal.setTime(date);
 cal.set(5, cal.getActualMaximum(5));
 cal.set(11, 0);
 cal.set(12, 0);
 cal.set(13, 0);
 cal.set(14, 0);
 return cal.getTime();
 }
 /**
 * 獲取當年的第一天
 * @param yearNum
 * @return
 */
 public static Date getYearFirstDay(int yearNum) {
 Calendar cal = Calendar.getInstance();
 cal.set(yearNum, 0, 1, 0, 0, 0);
 cal.set(14, 0);
 return cal.getTime();
 }
 /**
 * 獲取下一年的第一天
 * @param yearNum
 * @return
 */
 public static Date getNextYearFirstDay(int yearNum) {
 Calendar cal = Calendar.getInstance();
 cal.set(yearNum, 12, 1, 0, 0, 0);
 cal.set(14, 0);
 return cal.getTime();
 }
 /**
 * 獲取當年的最后一天
 * @param yearNum
 * @return
 */
 public static Date getYearEndDay(int yearNum) {
 Calendar cal = Calendar.getInstance();
 cal.set(yearNum, 12, 0, 0, 0, 0);
 cal.set(14, 0);
 return cal.getTime();
 }
 /**
 * 獲取當前星期
 * @param strDate
 * @param weekNum
 * @return
 */
 public static String getWeek(String strDate, int weekNum) {
 Calendar c = Calendar.getInstance();
 c.setTime(parseDate(strDate));
 if (weekNum == 1)
  c.set(7, 2);
 else if (weekNum == 2)
  c.set(7, 3);
 else if (weekNum == 3)
  c.set(7, 4);
 else if (weekNum == 4)
  c.set(7, 5);
 else if (weekNum == 5)
  c.set(7, 6);
 else if (weekNum == 6)
  c.set(7, 7);
 else if (weekNum == 0)
  c.set(7, 1);
 return formatDate(c.getTime());
 }
 public static Date getWeek(Date date, int weekNum) {
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 if (weekNum == 1)
  c.set(7, 2);
 else if (weekNum == 2)
  c.set(7, 3);
 else if (weekNum == 3)
  c.set(7, 4);
 else if (weekNum == 4)
  c.set(7, 5);
 else if (weekNum == 5)
  c.set(7, 6);
 else if (weekNum == 6)
  c.set(7, 7);
 else if (weekNum == 0)
  c.set(7, 1);
 return c.getTime();
 }
 /**
 * 下個月日期
 * @param date
 * @return
 */
 public static Date getNextMonday(Date date) {
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 do
  c.add(Calendar.DAY_OF_MONTH, 1);
 while (c.get(Calendar.DAY_OF_WEEK) != 2);
 return c.getTime();
 }
 /**
 * 獲得某一日期的前一天
 *
 */
 public static Date getPreviousDate(Date date) {
 Calendar calendar = Calendar.getInstance();
 calendar.setTime(date);
 calendar.add(Calendar.DATE, -1);
 calendar.set(Calendar.HOUR, 0);
 calendar.set(Calendar.SECOND, 0);
 calendar.set(Calendar.MINUTE, 0);
 calendar.set(Calendar.MILLISECOND, 0);
 return getSqlDate(calendar.getTime());
 }
 /**
 * 獲得某年某月最后一天的日期
 *
 */
 public static Date getLastDayOfMonth(int year, int month) {
 Calendar calendar = Calendar.getInstance();
 calendar.set(Calendar.YEAR, year);
 calendar.set(Calendar.MONTH, month);
 calendar.set(Calendar.DATE, 1);
 return getPreviousDate(getSqlDate(calendar.getTime()));
 }
 /**
 * 獲取一個月的天數
 * @param year
 * @param month
 * @return
 */
 public static int getDaysInMonth(int year, int month) {
 Calendar cal = Calendar.getInstance();
 cal.set(Calendar.YEAR, year);
 cal.set(Calendar.MONTH, month - 1);// Java月份才0開始算
 return cal.getActualMaximum(Calendar.DATE);
 }
 //----------------根據用戶生日計算年齡-------------------------------------------------
 /**
 * 根據用戶生日計算年齡
 */
 public static int getAgeByBirthday(Date birthday) {
 Calendar cal = Calendar.getInstance();
 if (cal.before(birthday)) {
  throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
 }
 int yearNow = cal.get(Calendar.YEAR);
 int monthNow = cal.get(Calendar.MONTH) + 1;
 int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
 cal.setTime(birthday);
 int yearBirth = cal.get(Calendar.YEAR);
 int monthBirth = cal.get(Calendar.MONTH) + 1;
 int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
 int age = yearNow - yearBirth;
 if (monthNow <= monthBirth) {
  if (monthNow == monthBirth) {
  if (dayOfMonthNow < dayOfMonthBirth) {
   age--;
  }
  } else {
  age--;
  }
 }
 return age;
 }
 /**
 * 由java.util.Date到java.sql.Date的類型轉換
 *
 */
 public static Date getSqlDate(java.util.Date date) {
 return new Date(date.getTime());
 }
 public static void main(String[] args) {
 //dd
 System.out.println("服務器之家測試結果:");
 System.out.println("2017年 2月最后一天日期為:"+getLastDayOfMonth(2017, 2));
 System.out.println("2017年第一天日期為:"+getYearFirstDay(2017));
 System.out.println("2017年最后一天日期為:"+getYearEndDay(2017)); }
}

運行結果:

Java日期操作方法工具類實例【包含日期比較大小,相加減,判斷,驗證,獲取年份等】

附:本例中用到了org.apache.http.util.TextUtils包,相關的jar包文件可點擊此處本站下載

希望本文所述對大家java程序設計有所幫助。

原文鏈接:http://blog.csdn.net/lovoo/article/details/77872912

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 手机看片自拍自自拍日韩免费 | 四虎影视在线看免费 720p | 久久日韩精品无码一区 | 美女脱了内裤打开腿让男人图片 | 精品无人区乱码1区2区3区在线 | 天天做天天爽天天谢 | 国语刺激对白勾搭视频在线观看 | 爸爸的宝贝小说全文在线阅读 | 亚洲麻豆精品果冻传媒 | 日本无卡码一区二区三区 | 男女真实无遮挡xx00动态图软件 | 男人操美女视频 | 男人使劲躁女人视频免费 | 欧美久久一区二区三区 | 色婷婷久久综合中文久久一本` | 亚洲精品网址 | 污网站免费观看在线高清 | 87影院在线观看视频在线观看 | 亚洲欧美一区二区久久 | 欧美午夜精品久久久久久黑人 | 精品国语国产在线对白 | 国产免费一区二区三区免费视频 | 婷射吧| 狠狠色综合久久婷婷 | 武侠艳妇屈辱的张开双腿 | 日韩大片在线播放 | 牛牛影院成人免费网页 | 毛片网站观看 | 国产美女下面流出白浆视频 | 国产日韩精品一区二区在线观看播放 | 亚洲色图150p | 国产精品自在线拍 | 国产精品反差婊在线观看 | 99re7在线精品免费视频 | 精品视频中文字幕 | 日本人添下面的全过程 | 日本视频在线观看播放 | 九九成人免费视频 | jazz欧美人免费xxxxxx | 国产亚洲sss在线播放 | 午夜国产精品福利在线观看 |