国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java日期處理工具類

java日期處理工具類

2020-04-30 10:01jiangbang JAVA教程

這篇文章主要為大家詳細介紹了java日期處理工具類,其次還介紹了日期處理的基礎知識,感興趣的小伙伴們可以參考一下

本文針對日期處理進行學習使用,主要分為兩部分,下面為大家具體介紹一下

第一部分:日期處理基礎知識

Date 類
作用:最主要的作用就是獲得當前時間
將日期轉換為標準格式

java" id="highlighter_345011">
?
1
2
3
4
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(date);
System.out.println(“2015-10-16 14:59:52”);

將String轉換為Date類型

?
1
2
3
4
String day = "2014-6-5 10:30:30";
SimpleDateFormat d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date2 = d2.parse(day);
System.out.println(“Thu Jun 05 10:30:30 CST 2014”);

Calendar 類的應用

java.util.Calendar 類是一個抽象類,可以通過調用 getInstance() 靜態方法獲取一個 Calendar 對象,此對象已由當前日期時間初始化,即默認代表當前時間

?
1
2
3
4
5
6
7
Calendar c = Calendar.getInstance();
int year = c.get(Calender.YEAR);
int month= c.get(Calender.MONTH)+1; //獲取月份,0表示1月份
int day = c.get(Calender.DAY_OF_MONTH);
int hour= c.get(Calender.HOUR_OF_DAY);
int minute= c.get(Calender.MINUTE);
int second = c.get(Calender.SECOND);

比較2個時間相差的月份

?
1
2
3
4
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateTime d1 = new DateTime(format.parse("2016-10-31 00:00:00"));
DateTime d2 = new DateTime(format.parse("2015-1-31 00:00:00"));
System.out.println(Math.abs(Months.monthsBetween(d1,d2).getMonths()));

第二部分:日期處理工具類

?
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
705
706
707
708
709
710
711
712
713
714
715
716
717
package com.analysys.website.control;
 
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
 
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
 
/**
 * 日期處理工具類
 * @author dylan_xu
 * @date Mar 11, 2012
 * @modified by
 * @modified date
 * @since JDK1.6
 * @see com.util.DateUtil
 */
  
public class DateUtil {
  // ~ Static fields/initializers
  // =============================================
  
  private static Logger logger = Logger.getLogger(DateUtil.class);
  private static String defaultDatePattern = null;
  private static String timePattern = "HH:mm";
  private static Calendar cale = Calendar.getInstance();
  public static final String TS_FORMAT = DateUtil.getDatePattern() + " HH:mm:ss.S";
  /** 日期格式yyyy-MM字符串常量 */
  private static final String MONTH_FORMAT = "yyyy-MM";
  /** 日期格式yyyy-MM-dd字符串常量 */
  private static final String DATE_FORMAT = "yyyy-MM-dd";
  /** 日期格式HH:mm:ss字符串常量 */
  private static final String HOUR_FORMAT = "HH:mm:ss";
  /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */
  private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
  /** 某天開始時分秒字符串常量 00:00:00 */
  private static final String DAY_BEGIN_STRING_HHMMSS = " 00:00:00";
  /** 某天結束時分秒字符串常量 23:59:59 */
  public static final String DAY_END_STRING_HHMMSS = " 23:59:59";
  private static SimpleDateFormat sdf_date_format = new SimpleDateFormat(DATE_FORMAT);
  private static SimpleDateFormat sdf_hour_format = new SimpleDateFormat(HOUR_FORMAT);
  private static SimpleDateFormat sdf_datetime_format = new SimpleDateFormat(DATETIME_FORMAT);
    
  
  // ~ Methods
  // ================================================================
  
  public DateUtil() {
  }
  
  /**
   * 獲得服務器當前日期及時間,以格式為:yyyy-MM-dd HH:mm:ss的日期字符串形式返回
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   */
  public static String getDateTime() {
    try {
      return sdf_datetime_format.format(cale.getTime());
    } catch (Exception e) {
      logger.debug("DateUtil.getDateTime():" + e.getMessage());
      return "";
    }
  }
  
  /**
   * 獲得服務器當前日期,以格式為:yyyy-MM-dd的日期字符串形式返回
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   */
  public static String getDate() {
    try {
      return sdf_date_format.format(cale.getTime());
    } catch (Exception e) {
      logger.debug("DateUtil.getDate():" + e.getMessage());
      return "";
    }
  }
  
  /**
   * 獲得服務器當前時間,以格式為:HH:mm:ss的日期字符串形式返回
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   */
  public static String getTime() {
    String temp = " ";
    try {
      temp += sdf_hour_format.format(cale.getTime());
      return temp;
    } catch (Exception e) {
      logger.debug("DateUtil.getTime():" + e.getMessage());
      return "";
    }
  }
  
  /**
   * 統計時開始日期的默認值
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   */
  public static String getStartDate() {
    try {
      return getYear() + "-01-01";
    } catch (Exception e) {
      logger.debug("DateUtil.getStartDate():" + e.getMessage());
      return "";
    }
  }
  
  /**
   * 統計時結束日期的默認值
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   */
  public static String getEndDate() {
    try {
      return getDate();
    } catch (Exception e) {
      logger.debug("DateUtil.getEndDate():" + e.getMessage());
      return "";
    }
  }
  
  /**
   * 獲得服務器當前日期的年份
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   */
  public static String getYear() {
    try {
      return String.valueOf(cale.get(Calendar.YEAR));
    } catch (Exception e) {
      logger.debug("DateUtil.getYear():" + e.getMessage());
      return "";
    }
  }
  
  /**
   * 獲得服務器當前日期的月份
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   */
  public static String getMonth() {
    try {
      java.text.DecimalFormat df = new java.text.DecimalFormat();
      df.applyPattern("00;00");
      return df.format((cale.get(Calendar.MONTH) + 1));
    } catch (Exception e) {
      logger.debug("DateUtil.getMonth():" + e.getMessage());
      return "";
    }
  }
  
  /**
   * 獲得服務器在當前月中天數
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   */
  public static String getDay() {
    try {
      return String.valueOf(cale.get(Calendar.DAY_OF_MONTH));
    } catch (Exception e) {
      logger.debug("DateUtil.getDay():" + e.getMessage());
      return "";
    }
  }
  
  /**
   * 比較兩個日期相差的天數
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param date1
   * @param date2
   * @return
   */
  public static int getMargin(String date1, String date2) {
    int margin;
    try {
      ParsePosition pos = new ParsePosition(0);
      ParsePosition pos1 = new ParsePosition(0);
      Date dt1 = sdf_date_format.parse(date1, pos);
      Date dt2 = sdf_date_format.parse(date2, pos1);
      long l = dt1.getTime() - dt2.getTime();
      margin = (int) (l / (24 * 60 * 60 * 1000));
      return margin;
    } catch (Exception e) {
      logger.debug("DateUtil.getMargin():" + e.toString());
      return 0;
    }
  }
  
  /**
   * 比較兩個日期相差的天數
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param date1
   * @param date2
   * @return
   */
  public static double getDoubleMargin(String date1, String date2) {
    double margin;
    try {
      ParsePosition pos = new ParsePosition(0);
      ParsePosition pos1 = new ParsePosition(0);
      Date dt1 = sdf_datetime_format.parse(date1, pos);
      Date dt2 = sdf_datetime_format.parse(date2, pos1);
      long l = dt1.getTime() - dt2.getTime();
      margin = (l / (24 * 60 * 60 * 1000.00));
      return margin;
    } catch (Exception e) {
      logger.debug("DateUtil.getMargin():" + e.toString());
      return 0;
    }
  }
  
  /**
   * 比較兩個日期相差的月數
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param date1
   * @param date2
   * @return
   */
  public static int getMonthMargin(String date1, String date2) {
    int margin;
    try {
      margin = (Integer.parseInt(date2.substring(0, 4)) - Integer.parseInt(date1.substring(0, 4))) * 12;
      margin += (Integer.parseInt(date2.substring(4, 7).replaceAll("-0",
          "-")) - Integer.parseInt(date1.substring(4, 7).replaceAll("-0", "-")));
      return margin;
    } catch (Exception e) {
      logger.debug("DateUtil.getMargin():" + e.toString());
      return 0;
    }
  }
  
  /**
   * 返回日期加X天后的日期
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param date
   * @param i
   * @return
   */
  public static String addDay(String date, int i) {
    try {
      GregorianCalendar gCal = new GregorianCalendar(
          Integer.parseInt(date.substring(0, 4)), 
          Integer.parseInt(date.substring(5, 7)) - 1
          Integer.parseInt(date.substring(8, 10)));
      gCal.add(GregorianCalendar.DATE, i);
      return sdf_date_format.format(gCal.getTime());
    } catch (Exception e) {
      logger.debug("DateUtil.addDay():" + e.toString());
      return getDate();
    }
  }
  
  /**
   * 返回日期加X月后的日期
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param date
   * @param i
   * @return
   */
  public static String addMonth(String date, int i) {
    try {
      GregorianCalendar gCal = new GregorianCalendar(
          Integer.parseInt(date.substring(0, 4)), 
          Integer.parseInt(date.substring(5, 7)) - 1
          Integer.parseInt(date.substring(8, 10)));
      gCal.add(GregorianCalendar.MONTH, i);
      return sdf_date_format.format(gCal.getTime());
    } catch (Exception e) {
      logger.debug("DateUtil.addMonth():" + e.toString());
      return getDate();
    }
  }
  
  /**
   * 返回日期加X年后的日期
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param date
   * @param i
   * @return
   */
  public static String addYear(String date, int i) {
    try {
      GregorianCalendar gCal = new GregorianCalendar(
          Integer.parseInt(date.substring(0, 4)), 
          Integer.parseInt(date.substring(5, 7)) - 1
          Integer.parseInt(date.substring(8, 10)));
      gCal.add(GregorianCalendar.YEAR, i);
      return sdf_date_format.format(gCal.getTime());
    } catch (Exception e) {
      logger.debug("DateUtil.addYear():" + e.toString());
      return "";
    }
  }
  
  /**
   * 返回某年某月中的最大天
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param year
   * @param month
   * @return
   */
  public static int getMaxDay(int iyear, int imonth) {
    int day = 0;
    try {
      if (imonth == 1 || imonth == 3 || imonth == 5 || imonth == 7
          || imonth == 8 || imonth == 10 || imonth == 12) {
        day = 31;
      } else if (imonth == 4 || imonth == 6 || imonth == 9 || imonth == 11) {
        day = 30;
      } else if ((0 == (iyear % 4)) && (0 != (iyear % 100)) || (0 == (iyear % 400))) {
        day = 29;
      } else {
        day = 28;
      }
      return day;
    } catch (Exception e) {
      logger.debug("DateUtil.getMonthDay():" + e.toString());
      return 1;
    }
  }
  
  /**
   * 格式化日期
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param orgDate
   * @param Type
   * @param Span
   * @return
   */
  @SuppressWarnings("static-access")
  public String rollDate(String orgDate, int Type, int Span) {
    try {
      String temp = "";
      int iyear, imonth, iday;
      int iPos = 0;
      char seperater = '-';
      if (orgDate == null || orgDate.length() < 6) {
        return "";
      }
  
      iPos = orgDate.indexOf(seperater);
      if (iPos > 0) {
        iyear = Integer.parseInt(orgDate.substring(0, iPos));
        temp = orgDate.substring(iPos + 1);
      } else {
        iyear = Integer.parseInt(orgDate.substring(0, 4));
        temp = orgDate.substring(4);
      }
  
      iPos = temp.indexOf(seperater);
      if (iPos > 0) {
        imonth = Integer.parseInt(temp.substring(0, iPos));
        temp = temp.substring(iPos + 1);
      } else {
        imonth = Integer.parseInt(temp.substring(0, 2));
        temp = temp.substring(2);
      }
  
      imonth--;
      if (imonth < 0 || imonth > 11) {
        imonth = 0;
      }
  
      iday = Integer.parseInt(temp);
      if (iday < 1 || iday > 31)
        iday = 1;
  
      Calendar orgcale = Calendar.getInstance();
      orgcale.set(iyear, imonth, iday);
      temp = this.rollDate(orgcale, Type, Span);
      return temp;
    } catch (Exception e) {
      return "";
    }
  }
  
  public static String rollDate(Calendar cal, int Type, int Span) {
    try {
      String temp = "";
      Calendar rolcale;
      rolcale = cal;
      rolcale.add(Type, Span);
      temp = sdf_date_format.format(rolcale.getTime());
      return temp;
    } catch (Exception e) {
      return "";
    }
  }
  
  /**
   * 返回默認的日期格式
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   */
  public static synchronized String getDatePattern() {
    defaultDatePattern = "yyyy-MM-dd";
    return defaultDatePattern;
  }
  
  /**
   * 將指定日期按默認格式進行格式代化成字符串后輸出如:yyyy-MM-dd
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param aDate
   * @return
   */
  public static final String getDate(Date aDate) {
    SimpleDateFormat df = null;
    String returnValue = "";
    if (aDate != null) {
      df = new SimpleDateFormat(getDatePattern());
      returnValue = df.format(aDate);
    }
    return (returnValue);
  }
  
  /**
   * 取得給定日期的時間字符串,格式為當前默認時間格式
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param theTime
   * @return
   */
  public static String getTimeNow(Date theTime) {
    return getDateTime(timePattern, theTime);
  }
  
  /**
   * 取得當前時間的Calendar日歷對象
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   * @throws ParseException
   */
  public Calendar getToday() throws ParseException {
    Date today = new Date();
    SimpleDateFormat df = new SimpleDateFormat(getDatePattern());
    String todayAsString = df.format(today);
    Calendar cal = new GregorianCalendar();
    cal.setTime(convertStringToDate(todayAsString));
    return cal;
  }
  
  /**
   * 將日期類轉換成指定格式的字符串形式
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param aMask
   * @param aDate
   * @return
   */
  public static final String getDateTime(String aMask, Date aDate) {
    SimpleDateFormat df = null;
    String returnValue = "";
  
    if (aDate == null) {
      logger.error("aDate is null!");
    } else {
      df = new SimpleDateFormat(aMask);
      returnValue = df.format(aDate);
    }
    return (returnValue);
  }
  
  /**
   * 將指定的日期轉換成默認格式的字符串形式
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param aDate
   * @return
   */
  public static final String convertDateToString(Date aDate) {
    return getDateTime(getDatePattern(), aDate);
  }
  
  /**
   * 將日期字符串按指定格式轉換成日期類型
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param aMask 指定的日期格式,如:yyyy-MM-dd
   * @param strDate 待轉換的日期字符串
   * @return
   * @throws ParseException
   */
  public static final Date convertStringToDate(String aMask, String strDate)
      throws ParseException {
    SimpleDateFormat df = null;
    Date date = null;
    df = new SimpleDateFormat(aMask);
  
    if (logger.isDebugEnabled()) {
      logger.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
    }
    try {
      date = df.parse(strDate);
    } catch (ParseException pe) {
      logger.error("ParseException: " + pe);
      throw pe;
    }
    return (date);
  }
  
  /**
   * 將日期字符串按默認格式轉換成日期類型
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param strDate
   * @return
   * @throws ParseException
   */
  public static Date convertStringToDate(String strDate)
      throws ParseException {
    Date aDate = null;
  
    try {
      if (logger.isDebugEnabled()) {
        logger.debug("converting date with pattern: " + getDatePattern());
      }
      aDate = convertStringToDate(getDatePattern(), strDate);
    } catch (ParseException pe) {
      logger.error("Could not convert '" + strDate + "' to a date, throwing exception");
      throw new ParseException(pe.getMessage(), pe.getErrorOffset());
    }
    return aDate;
  }
  
  /**
   * 返回一個JAVA簡單類型的日期字符串
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   */
  public static String getSimpleDateFormat() {
    SimpleDateFormat formatter = new SimpleDateFormat();
    String NDateTime = formatter.format(new Date());
    return NDateTime;
  }
    
  /**
   * 將指定字符串格式的日期與當前時間比較
   * @author DYLAN
   * @date Feb 17, 2012
   * @param strDate 需要比較時間
   * @return
   *   <p>
   *   int code
   *   <ul>
   *   <li>-1 當前時間 < 比較時間 </li>
   *   <li> 0 當前時間 = 比較時間 </li>
   *   <li>>=1當前時間 > 比較時間 </li>
   *   </ul>
   *   </p>
   */
  public static int compareToCurTime (String strDate) {
    if (StringUtils.isBlank(strDate)) {
      return -1;
    }
    Date curTime = cale.getTime();
    String strCurTime = null;
    try {
      strCurTime = sdf_datetime_format.format(curTime);
    } catch (Exception e) {
      if (logger.isDebugEnabled()) {
        logger.debug("[Could not format '" + strDate + "' to a date, throwing exception:" + e.getLocalizedMessage() + "]");
      }
    }
    if (StringUtils.isNotBlank(strCurTime)) {
      return strCurTime.compareTo(strDate);
    }
    return -1;
  }
    
  /**
   * 為查詢日期添加最小時間
   *
   * @param 目標類型Date
   * @param 轉換參數Date
   * @return
   */
  @SuppressWarnings("deprecation")
  public static Date addStartTime(Date param) {
    Date date = param;
    try {
      date.setHours(0);
      date.setMinutes(0);
      date.setSeconds(0);
      return date;
    } catch (Exception ex) {
      return date;
    }
  }
  
  /**
   * 為查詢日期添加最大時間
   *
   * @param 目標類型Date
   * @param 轉換參數Date
   * @return
   */
  @SuppressWarnings("deprecation")
  public static Date addEndTime(Date param) {
    Date date = param;
    try {
      date.setHours(23);
      date.setMinutes(59);
      date.setSeconds(0);
      return date;
    } catch (Exception ex) {
      return date;
    }
  }
  
  /**
   * 返回系統現在年份中指定月份的天數
   *
   * @param 月份month
   * @return 指定月的總天數
   */
  @SuppressWarnings("deprecation")
  public static String getMonthLastDay(int month) {
    Date date = new Date();
    int[][] day = { { 0, 30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
        { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
    int year = date.getYear() + 1900;
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
      return day[1][month] + "";
    } else {
      return day[0][month] + "";
    }
  }
  
  /**
   * 返回指定年份中指定月份的天數
   *
   * @param 年份year
   * @param 月份month
   * @return 指定月的總天數
   */
  public static String getMonthLastDay(int year, int month) {
    int[][] day = { { 0, 30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
        { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
      return day[1][month] + "";
    } else {
      return day[0][month] + "";
    }
  }
  
  /**
   * 判斷是平年還是閏年
   * @author dylan_xu
   * @date Mar 11, 2012
   * @param year
   * @return
   */
  public static boolean isLeapyear(int year) {
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0) {
      return true;
    } else {
      return false;
    }
  }
  
  /**
   * 取得當前時間的日戳
   * @author dylan_xu
   * @date Mar 11, 2012
   * @return
   */
  @SuppressWarnings("deprecation")
  public static String getTimestamp() {
    Date date = cale.getTime();
    String timestamp = "" + (date.getYear() + 1900) + date.getMonth()
        + date.getDate() + date.getMinutes() + date.getSeconds()
        + date.getTime();
    return timestamp;
  }
  
  /**
   * 取得指定時間的日戳
   *
   * @return
   */
  @SuppressWarnings("deprecation")
  public static String getTimestamp(Date date) {
    String timestamp = "" + (date.getYear() + 1900) + date.getMonth()
        + date.getDate() + date.getMinutes() + date.getSeconds()
        + date.getTime();
    return timestamp;
  }
  
  public static void main(String[] args) {
    System.out.println(getYear() + "|" + getMonth() + "|" + getDate());
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 淫片免费观看 | 国产精品自产拍在线观看 | 欧美日本精品 | 亚洲综合激情网 | 成人午夜视频在线观看 | 日韩视频一区 | 免费观看黄视频 | 精品一区二区三区免费毛片爱 | 久久久毛片 | 欧美成人精品一区二区三区在线看 | 中文字幕日韩欧美一区二区三区 | 精品国产一区二区国模嫣然 | 中文字幕精品一区二区精品 | 中文字幕乱码亚洲精品 | 亚洲精品久久久久久久久久久久久 | 91精品一久久香蕉国产线看观看新通道出现 | 国产在线观看一区 | av网站在线播放 | 亚洲欧美成人综合 | 国产一区二区三区久久 | 久久久精品一区 | 久久精品国产亚洲 | 亚洲国产精品一区二区三区 | 青青草免费在线 | 国产一级片儿 | 免费一级欧美在线观看视频 | 成人久久久久久久 | 久久久亚洲精品中文字幕 | 日韩中文字幕在线视频 | 国产精品成人一区二区三区夜夜夜 | 欧美日韩电影 | 99久久免费精品国产男女性高好 | 亚洲精品成人在线 | 狠狠综合 | 午夜精| 日本伊人网 | 天天操天天碰 | 久久久一二三 | 国产精品观看 | 色视频在线免费观看 | 九九热在线免费视频 |