DateTool.java
19.3 KB
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
718
719
720
721
722
package com.bjivt.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;
@SuppressWarnings({ "deprecation", "finally" })
public final class DateTool {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar ca = null;
public DateTool() {
ca = Calendar.getInstance();
}
public static DateTool getInstance() {
return new DateTool();
}
public static Date stringToDate(String date) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(date);
} catch (Exception err) {
return null;
}
}
public static Date stringToDate(String date, String srcFormat) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(srcFormat);
return sdf.parse(date);
} catch (Exception err) {
return null;
}
}
public Date stringToTime(String date) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.parse(date);
} catch (Exception err) {
return null;
}
}
public static String countDate(int dateNum) {
String date = null;
try {
Calendar ca = Calendar.getInstance();
ca.add(5, dateNum);
date = ca.get(1) + "-" + (ca.get(2) + 1) + "-" + ca.get(5);
} catch (Exception err) {
err.printStackTrace();
}
return date;
}
public static String dateToString(Date date) {
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
return simpledate.format(date);
}
public static String dateToString(Date date, String destFormat) {
SimpleDateFormat simpledate = new SimpleDateFormat(destFormat);
return simpledate.format(date);
}
public String dateToTimeString(Date date) {
ca.setTime(new java.util.Date());
SimpleDateFormat simpledate = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return simpledate.format(date);
}
public String dateToTimeString() {
SimpleDateFormat simpledate = new SimpleDateFormat("yyyyMMddHHmmss");
return simpledate.format(new java.util.Date());
}
public String getYearMonthDay(String dateFormateArg) {
String dateFormate = null;
if (dateFormateArg != null && !"".equals(dateFormateArg)) {
dateFormate = dateFormateArg;
} else {
dateFormate = "yyyyMMdd";
}
ca.setTime(new java.util.Date());
SimpleDateFormat simpledate = new SimpleDateFormat(dateFormate);
String date = simpledate.format(ca.getTime());
return date;
}
public int getYear() {
ca.setTime(new java.util.Date());
int year = ca.get(Calendar.YEAR);
return year;
}
public int getMonth() {
ca.setTime(new java.util.Date());
int month = ca.get(Calendar.MONTH);
return month;
}
public int getDay() {
ca.setTime(new java.util.Date());
int day = ca.get(Calendar.DAY_OF_MONTH);
return day;
}
public int getHour() {
ca.setTime(new java.util.Date());
return ca.get(Calendar.HOUR_OF_DAY);
}
public String getBeforeTime(String datetime) {
String[] dates = StringTool.splitString(datetime, "-");
ca.set(Calendar.YEAR, Integer.parseInt(dates[0]));
ca.set(Calendar.MONTH, Integer.parseInt(dates[1]) - 1);
ca.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dates[2]) + 1);
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
return simpledate.format(ca.getTime());
}
public int getDistanceHour(Date date1, Date date2) {
Calendar ca1 = Calendar.getInstance();
Calendar ca2 = Calendar.getInstance();
ca1.setTime(date1);
ca2.setTime(date2);
int distanceHour = ca2.get(Calendar.HOUR_OF_DAY)
- ca1.get(Calendar.HOUR_OF_DAY);
distanceHour = Math.abs(distanceHour
+ Math.abs((date2.getDay() - date1.getDay()) * 24));
return distanceHour;
}
public static String getBeforeTime(String deftime, String oldfmt,
int timediff, String newfmt) {
String rq = null;
try {
try {
if (deftime == null || deftime.equals("")) {
System.out
.println("Error: Method: DateUtls.getBeforeTime :Incorrect para");
String s = rq;
return s;
}
Calendar cal = getCal(deftime, oldfmt);
if (cal == null) {
System.out
.println("Error: Method: DateUtls.getBeforeTime :Incorrect Calendar");
String s1 = rq;
return s1;
}
cal.add(12, -timediff * 60);
SimpleDateFormat sDateformat = new SimpleDateFormat(newfmt);
rq = sDateformat.format(cal.getTime());
} catch (Exception e) {
System.out
.println("Fatal: Method: DateUtls.getBeforeTime :"
.concat(String.valueOf(String.valueOf(e
.getMessage()))));
}
} finally {
return rq;
}
}
public static Calendar getCal(String strdate, String fmt) {
Calendar cal = null;
try {
try {
if (strdate == null || fmt == null) {
System.out
.println("Error: Method: DateUtls.getCal :Incorrect para");
Calendar calendar = cal;
return calendar;
}
SimpleDateFormat sDateformat = new SimpleDateFormat(fmt);
Date myDate = sDateformat.parse(strdate, new ParsePosition(0));
if (myDate == null) {
System.out
.println("Fatal: Method: DateUtls.getCal :Incorrect Parse");
Calendar calendar1 = cal;
return calendar1;
}
cal = Calendar.getInstance();
cal.clear();
cal.setTime(myDate);
} catch (Exception e) {
System.out
.println("Error: Method: DateUtls.getCal :"
.concat(String.valueOf(String.valueOf(e
.getMessage()))));
}
} finally {
return cal;
}
}
/**
* 距离指定的时间date的差时
*
* @param date
* @return
*/
public static String getDate(Date date) {
int second = 1000;
int minutes = second * 60;
long hours = minutes * 60;
long days = hours * 24;
long week = days * 7;
DateFormat formatlong = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatlong.format(date);
Date nowtime = new Date();
formatlong.format(nowtime);
long longtime = nowtime.getTime() - date.getTime();
if (longtime > (long) days * 365) {
return (int) Math.floor(longtime / days * 365) + "年前";
} else if (longtime > (long) days * 30) {
return (int) Math.floor(longtime / (long) days * 30) + "月前";
} else if (longtime > week) {
return (int) Math.floor(longtime / week) + "周前";
} else if (longtime > (long) days) {
return (int) Math.floor(longtime / (long) days) + "天前";
} else if (longtime > hours) {
return (int) Math.floor(longtime / hours) + "小时";
} else if (longtime > minutes) {
return (int) Math.floor(longtime / minutes) + "分钟";
} else if (longtime > second) {
return (int) Math.floor(longtime / second) + "秒前";
} else {
return "刚刚..";
}
}
/**
* leil get String date;
*
* @return
*/
public static String DateNow() {
try {
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return format.format(new Date());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* leil get String day;
*
* @return
*/
public static String DateDay() {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(new Date());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* leil get String day;
*
* @return
*/
public static String getNowDate_yyyyMMdd() {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(new Date());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* leil get String year;
*
* @return
*/
public static String year() {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy");
return format.format(new Date());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
*
* 获得两个时间 时差 天数
*/
public static int getTimeQuantumDay(String time1, String time2) {
SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
Date date1 = null;
Date date2 = null;
try {
date1 = ft.parse(time1);
date2 = ft.parse(time2);
} catch (ParseException e) {
e.printStackTrace();
}
return getTimeQuantumDay(date1, date2);
}
public static int getTimeQuantumDay(Date date1, Date date2) {
long quantum = 0;
quantum = date1.getTime() - date2.getTime();
quantum = quantum / 1000 / 60 / 60 / 24;
return (int) quantum;
}
/**
* 输入指定时间最末时间点 相应起始时间其默认值即是(即作相应格式化即可)
*
* @param date
* 指定的时间
* @param srcFormat
* 原格式
* @param destFormat
* 输出格式
* @return String
*/
public static String getEndTime(String date, String srcFormat,
String destFormat) {
SimpleDateFormat sdf1 = new SimpleDateFormat(srcFormat);
SimpleDateFormat sdf2 = new SimpleDateFormat(destFormat);
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf1.parse(date));
if (Pattern.matches("^yyyy-MM-dd HH:mm$", srcFormat)) {
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH),
c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), 59);
return sdf2.format(c.getTime());
} else if (Pattern.matches("^yyyy-MM-dd HH$", srcFormat)) {
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH),
c.get(Calendar.HOUR_OF_DAY), 59, 59);
return sdf2.format(c.getTime());
} else if (Pattern.matches("^yyyy-MM-dd$", srcFormat)) {
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
return sdf2.format(c.getTime());
} else if (Pattern.matches("^yyyy-MM$", srcFormat)) {
int maxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), maxDay, 23,
59, 59);
return sdf2.format(c.getTime());
} else if (Pattern.matches("^yyyy$", srcFormat)) {
c.set(Calendar.MONTH, 11);
int maxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), maxDay, 23,
59, 59);
return sdf2.format(c.getTime());
}
} catch (ParseException e) {
e.printStackTrace();
}
return sdf2.format(c.getTime());
}
/**
* 改变时间格式
*
* @param date
* 时间
* @param srcFormat
* 原来的格式
* @param destFormat
* 输出的格式
*/
public static String changeFormat(String date, String srcFormat,
String destFormat) {
SimpleDateFormat sdf1 = new SimpleDateFormat(srcFormat);
SimpleDateFormat sdf2 = new SimpleDateFormat(destFormat);
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf1.parse(date));
} catch (ParseException e) {
e.printStackTrace();
return null;
}
return sdf2.format(c.getTime());
}
/**
* 获得当前时间为基准的前几个自然月
*
* @param n
* 向前推几个月
* @param format
* 输出格式
*/
public static String getPreMonth(int n, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -n); // 得到前一个月
return sdf.format(calendar.getTime());
}
/**
* 获得当前时间为基准的后几个自然月
*
* @param n
* 向后推几个月
* @param format
* 输出格式
*/
public static String getSufMonth(int n, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, n); // 得到后一个月
return sdf.format(calendar.getTime());
}
public static String getPreMonth(Date date, int n, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, -n); // 得到前一个月
return sdf.format(calendar.getTime());
}
public static String getPreYear(Date date, int n, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, -n); // 得到前几年
return sdf.format(calendar.getTime());
}
public static String getPreMinute(Date date, int n, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, n); // 得到前一个月
return sdf.format(calendar.getTime());
}
/**
* 得到传入的日期的月最后一天 的日期
*
* @param sDate1
* @return
*/
public static Date getLastDayOfMonth(Date sDate1) {
Calendar cDay1 = Calendar.getInstance();
cDay1.setTime(sDate1);
final int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
Date lastDate = cDay1.getTime();
lastDate.setDate(lastDay);
return lastDate;
}
public static int getMaxDayofMonth(Date date) {
Calendar cDay1 = Calendar.getInstance();
cDay1.setTime(date);
final int maxDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
return maxDay;
}
public static int getAge(String birthDate) {
if (birthDate == null)
throw new
RuntimeException("出生日期不能为null");
int age = 0;
Date now = new Date();
SimpleDateFormat format_y = new
SimpleDateFormat("yyyy");
SimpleDateFormat format_M = new
SimpleDateFormat("MM");
String birth_year =birthDate.split("-")[0];
String this_year =
format_y.format(now);
String birth_month =birthDate.split("-")[1];
String this_month =
format_M.format(now);
// 初步,估算
age =
Integer.parseInt(this_year) - Integer.parseInt(birth_year);
// 如果未到出生月份,则age - 1
if
(this_month.compareTo(birth_month) < 0)
age -=
1;
if (age <
0)
age =
0;
return age;
}
public static String getMondayOfThisWeek() {
Calendar c = Calendar.getInstance();
int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
if (day_of_week == 0)
day_of_week = 7;
c.add(Calendar.DATE, -day_of_week + 1);
return sdf.format(c.getTime());
}
//第二行中为1则是后一天,为-1则是前一天。
public static String getBeforeDay(){
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -1);
// Date date=calendar.getTime();
String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
return dayBefore;
}
//获取今天的日期
public static String getCurrDay(){
SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
return sp.format(new Date());
}
//获取当前月的开始时间
public static String getMinMonthDate(){
Calendar cal = Calendar.getInstance();
try {
cal.setTime(DateFormat.getDateInstance().parse(getCurrDay()));
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
return DateFormat.getDateInstance().format(cal.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获得指定日期的前N天
*
* @param specifiedDay
* @return
* @throws Exception
*/
public static String getSpecifiedDayBefore(String specifiedDay, int before) {
// SimpleDateFormat simpleDateFormat = new
// SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - before);
String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayBefore;
}
/**
* 把一个毫秒级时长转换成00:00:00格式
* @Title: formatTime
* @Description:
* @author Yh.T
*/
public static String formatMillTime(long time)
{
time = time/ 1000;
String strHour = "" + (time/3600);
String strMinute = "" + time%3600/60;
String strSecond = "" + time%3600%60;
strHour = strHour.length() < 2? "0" + strHour: strHour;
strMinute = strMinute.length() < 2? "0" + strMinute: strMinute;
strSecond = strSecond.length() < 2? "0" + strSecond: strSecond;
String strRsult = "";
if (!strHour.equals("00"))
{
strRsult += strHour + ":";
}
if (!strMinute.equals("00"))
{
strRsult += strMinute + ":";
}
strRsult += strSecond;
return strRsult;
}
/**
* 把一个秒级时长转换成00:00:00格式
* @Title: formatTime
* @Description:
* @author Yh.T
*/
public static String formatTime(long time)
{
String strHour = "" + (time/3600);
String strMinute = "" + time%3600/60;
String strSecond = "" + time%3600%60;
strHour = strHour.length() < 2? "0" + strHour: strHour;
strMinute = strMinute.length() < 2? "0" + strMinute: strMinute;
strSecond = strSecond.length() < 2? "0" + strSecond: strSecond;
String strRsult = "";
if (!strHour.equals("00"))
{
strRsult += strHour + ":";
}
if (!strMinute.equals("00"))
{
strRsult += strMinute + ":";
}
strRsult += strSecond;
return strRsult;
}
/**
* 比对时间
* @author
* @throws
*/
public static int dateCompare(String source, String traget, String type) throws Exception {
int ret = 2;
SimpleDateFormat format = new SimpleDateFormat(type);
Date sourcedate = format.parse(source);
Date tragetdate = format.parse(traget);
ret = sourcedate.compareTo(tragetdate);
return ret;
}
/**
* 比较时间
* @Title: dateCompare
* @Description: TODO(这里用一句话描述这个方法的作用)
* @author Yh.T
*/
public static int dateCompare(Date source, Date traget, String type) throws Exception {
int ret = 2;
SimpleDateFormat format = new SimpleDateFormat(type);
Date sourcedate = format.parse(format.format(source));
Date tragetdate = format.parse(format.format(traget));
ret = sourcedate.compareTo(tragetdate);
return ret;
}
public static void main(String[] args) throws ParseException {
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date d = sdf.parse("2013-09-11");
// Date now = new Date();
// System.out.println(sdf2.format(now));
// System.out.println(now.getTime());
// System.out.println(sdf2.format(d));
// System.out.println(d.getTime());
// System.out.println(getEndTime("2013", "yyyy", "yyyy-MM-dd HH:mm:ss"));
// Date dd = stringToDate("2013", "yyyy");
// System.out.println(dd.getTime());
// Date ddd = stringToDate("2013-01", "yyyy-MM");
// System.out.println(ddd.getTime());
//
// System.out.println(getPreMonth(1, "yyyy-MM"));
// System.out.println(getSufMonth(1, "yyyy-MM"));
// System.out.println(DateDay());
// stringToDate("2013", "yyyy-MM");
try {
String dd = DateTool.getPreMinute(new Date(), -1,"yyyyMMddHHmm");
String dr = DateTool.getPreMinute(new Date(), 1,"yyyyMMddHHmm");
int ret = DateTool.dateCompare("2013-09-12", "2013-09-11", "yyyy-MM-dd");
System.out.println(dd+"====="+dr);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 返回提前天数的时间
public static Date getBeforeDate(int skip) {
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -skip);
return c.getTime();
}
}