java计算时间差,⽇期差⼩结
转⾃:
1、java 7中的⽇历类Calendar
Calendar类使⽤其静态的getInstance()⽅法获取⼀个⽇历实例,该实例为当前的时间;如果想改变时间,可以通过其setTime⽅法传⼊⼀个Date对象,即可获得Date对象所表⽰时间的Calendar对象
1/**
2 *使⽤Calendar对象计算时间差,可以按照需求定制⾃⼰的计算逻辑
3 * @param strDate
4 * @throws ParseException
5*/
6public static void calculateTimeDifferenceByCalendar(String strDate) throws ParseException {
7    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
8    Date date = formatter.parse(strDate);
9
10    Calendar c1 = Instance();  //当前⽇期
11    Calendar c2 = Instance();
12    c2.setTime(date);  //设置为另⼀个时间
13
14int year = c1.get(Calendar.YEAR);
15int oldYear = c2.get(Calendar.YEAR);
16
17//这⾥只是简单的对两个年份数字进⾏相减,⽽没有考虑⽉份的情况
18    System.out.println("传⼊的⽇期与今年的年份差为:" + (year - oldYear));
19 }
输出为:
1传⼊的⽇期与今年的年份差为:12
2、java 8中的周期类Period
通过调⽤Period类的静态⽅法between,传⼊两个待⽐较的LocalDate对象today与oldDate,得到的Period的对象p中就包含了today与oldDate两个⽇期相差的年、⽉、⽇信息,可以通过p.getYears()等⽅法取出
1/**
2 * 使⽤java 8的Period的对象计算两个LocalDate对象的时间差,严格按照年、⽉、⽇计算,如:2018-03-12 与 2014-05-2
3 相差 3 年 9 个⽉ 17 天
3 * @param year
4 * @param month
5 * @param dayOfMonth
6*/
7public static void calculateTimeDifferenceByPeriod(int year, Month month, int dayOfMonth) {
8    LocalDate today = w();
9    System.out.println("Today:" + today);
10    LocalDate oldDate = LocalDate.of(year, month, dayOfMonth);
11    System.out.println("OldDate:" + oldDate);
12
13    Period p = Period.between(oldDate, today);
printf输出格式java14    System.out.printf("⽬标⽇期距离今天的时间差:%d 年 %d 个⽉ %d 天\n", p.getYears(), p.getMonths(), p.getDays());
15 }
输出为:
1 Today:2018-03-13
2 OldDate:2014-05-23
3⽬标⽇期距离今天的时间差:3 年 9 个⽉ 18 天
3、java 8中的Duration类
Duration与Period相对应,Period⽤于处理⽇期,⽽Duration计算时间差还可以处理具体的时间,也是通过调⽤其静态的between⽅法,该⽅法的签名是between(Temporal startInclusive, Temporal endExclusive),因此可以传⼊两个Instant的实例(Instant实现了Temporal接⼝),并可以以毫秒(toMillis)、秒(getSeconds)等多种形式表⽰得到的时间差
1public static void calculateTimeDifferenceByDuration() {
2    Instant inst1 = w();  //当前的时间
3    System.out.println("Inst1:" + inst1);
4    Instant inst2 = inst1.plus(Duration.ofSeconds(10));    //当前时间+10秒后的时间
5    System.out.println("Inst2:" + inst2);
6    Instant inst3 = inst1.plus(Duration.ofDays(125));      //当前时间+125天后的时间
7    System.out.println("inst3:" + inst3);
8
9    System.out.println("以毫秒计的时间差:" + Duration.between(inst1, inst2).toMillis());
10
11    System.out.println("以秒计的时间差:" + Duration.between(inst1, inst3).getSeconds());
12 }
输出为:
1 Inst1:2018-03-13T09:06:00.691Z
2 Inst2:2018-03-13T09:06:10.691Z
3 inst3:2018-07-16T09:06:00.691Z
4以毫秒计的时间差:10000
5以秒计的时间差:10800000
4、java 8中的ChronoUnit类
ChronoUnit的between⽅法签名为,between(Temporal temporal1Inclusive, Temporal temporal2Exclusive),需要注意的是,如果要以不同的单位展⽰时间差,between⼊参中的时间对象必须包含有对应的时间信息,否则会抛出
poral.UnsupportedTemporalTypeException: Unsupported unit XXX的异常
1/**
2 * 使⽤java 8的ChronoUnit,ChronoUnit可以以多种单位(基本涵盖了所有的,看源码发现竟然还有“F
OREVER”这种单位。。)表⽰两个时间的时间差
3*/
4public static void calculateTimeDifferenceByChronoUnit() {
5    LocalDate startDate = LocalDate.of(2003, Month.MAY, 9);
6    System.out.println("开始时间:" + startDate);
7
8    LocalDate endDate = LocalDate.of(2015, Month.JANUARY, 26);
9    System.out.println("结束时间:" + endDate);
10
11long daysDiff = ChronoUnit.DAYS.between(startDate, endDate);
12    System.out.println("两个时间之间的天数差为:" + daysDiff);
13//  long hoursDiff = ChronoUnit.HOURS.between(startDate, endDate);  //这句会抛出异常,因为LocalDate表⽰的时间中不包含时分秒等信息
14 }
输出为:
开始时间:2003-05-09
结束时间:2015-01-26
两个时间之间的天数差为:4280
5、传统的SimpleDateFormat类
⽤SimpleDateFormat计算时间差的⽅法,⽹上了⼀份,⾃⼰跑了⼀遍,可以使⽤,贴在下⾯
1/**
2* ⽤SimpleDateFormat计算时间差
3* @throws ParseException
4*/
5public static void calculateTimeDifferenceBySimpleDateFormat() throws ParseException {
6    SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
7/*天数差*/
8    Date fromDate1 = simpleFormat.parse("2018-03-01 12:00");
9    Date toDate1 = simpleFormat.parse("2018-03-12 12:00");
10long from1 = Time();
11long to1 = Time();
12int days = (int) ((to1 - from1) / (1000 * 60 * 60 * 24));
13    System.out.println("两个时间之间的天数差为:" + days);
14
15/*⼩时差*/
16    Date fromDate2 = simpleFormat.parse("2018-03-01 12:00");
17    Date toDate2 = simpleFormat.parse("2018-03-12 12:00");
18long from2 = Time();
19long to2 = Time();
20int hours = (int) ((to2 - from2) / (1000 * 60 * 60));
21    System.out.println("两个时间之间的⼩时差为:" + hours);
22
23/*分钟差*/
24    Date fromDate3 = simpleFormat.parse("2018-03-01 12:00");
25    Date toDate3 = simpleFormat.parse("2018-03-12 12:00");
26long from3 = Time();
27long to3 = Time();
28int minutes = (int) ((to3 - from3) / (1000 * 60));
29    System.out.println("两个时间之间的分钟差为:" + minutes);
30 }
输出为:
1两个时间之间的天数差为:11
2两个时间之间的⼩时差为:264
3两个时间之间的分钟差为:15840
总结
传统的SimpleDateFormat和Java 7中的Calendar在使⽤的时候需要⾃⼰写⼀个计算时间差的逻辑,⽐较⿇烦,但是却⽐较灵活,⽅便根据⾃⼰具体的需求来定制(⽐如,我想两个⽇期的天数差15天就算满⼀个⽉,不满15天不算⼀个⽉,如2018-01-04到2018-02-20,算2个⽉的时间差);⽽Java 8中的⼏个计算时间差的类更加⽅便、精确,可以以不同的单位表⽰得到的时间差,但要注意⼏个类所包含的时间信息的区别:
1 System.out.w());        //只包含⽇期信息
2 System.out.w());        //只包含时间信息
3 System.out.w());        //包含⽇期、时间信息
输出为:
1 2018-03-13
2 17:13:26.134
3 2018-03-13T17:13:26.135
以上总结的⼏个⽅法只是个例⼦,具体使⽤的时候可能需要传⼊⼀个或者两个时间进⾏⽐较,可能会涉及到这些时间对象的相互转
换,Instant、Date、LocalDate等等。。。我就不列举了。。。
另外在使⽤SimpleDateFormat对String类型的⽇期进⾏parse的时候,如果传⼊的⽇期为:2017-08-60,这种错误的⽇期,Java默认会按照⽇期的信息对其进⾏转换,formatter.parse("2017-08-60");,得到的⽇期为2017-09-29,⽽如果不想进⾏这种转换,⽽直接将其判定为输⼊错误,则可以设置formatter.setLenient(false);,这时就会抛出ParseException异常了
1 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
2 formatter.setLenient(false);
3try {
4    Date date = formatter.parse("2017-08-60");  //抛出转换异常
5    System.out.println(date);
6 } catch (ParseException e) {
7    e.printStackTrace();
8 }