Java8中计算日期时间差
https://blog.csdn.net/hspingcc/article/details/73332526
____________________________________________________________________________________________________________
一.简述
在Java8中,我们可以使用以下类来计算日期时间差异:
1.Period
2.Duration
3.ChronoUnit
二.Period类
主要是Period类方法getYears(),getMonths()和getDays()来计算.
1
示例:
package insping;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
public class Test {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println("Today : " + today);
LocalDate birthDate = LocalDate.of(1993, Month.OCTOBER, 19);
System.out.println("BirthDate : " + birthDate);
Period p = Period.between(birthDate, today);
System.out.printf("年龄 : %d 年 %d 月 %d 日", p.getYears(), p.getMonths(), p.getDays());
}
}
结果:
Today : 2017-06-16
BirthDate : 1993-10-19
年龄 : 23 年 7 月 28 日
三.Duration类
提供了使用基于时间的值(如秒,纳秒)测量时间量的方法。
示例:
package insping;
import java.time.Duration;
import java.time.Instant;
public class Test {
public static void main(String[] args) {
Instant inst1 = Instant.now();
System.out.println("Inst1 : " + inst1);
Instant inst2 = inst1.plus(Duration.ofSeconds(10));
System.out.println("Inst2 : " + inst2);
System.out.println("Difference in milliseconds : " + Duration.between(inst1, inst2).toMillis());
System.out.println("Difference in seconds : " + Duration.between(inst1, inst2).getSeconds());
}
}
结果:
Inst1 : 2017-06-16T07:46:45.085Z
Inst2 : 2017-06-16T07:46:55.085Z
Difference in milliseconds : 10000
Difference in seconds : 10
四.ChronoUnit类
ChronoUnit类可用于在单个时间单位内测量一段时间,例如天数或秒。
以下是使用between()方法来查找两个日期之间的区别的示例。
package insping;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
public class Test {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(1993, Month.OCTOBER, 19);
System.out.println("开始时间 : " + startDate);
LocalDate endDate = LocalDate.of(2017, Month.JUNE, 16);
System.out.println("结束时间 : " + endDate);
long daysDiff = ChronoUnit.DAYS.between(startDate, endDate);
System.out.println("两天之间的差在天数 : " + daysDiff);
}
}
结果:
开始时间 : 1993-10-19
结束时间 : 2017-06-16
两天之间的差在天数 : 8641
版权声明:本文为CSDN博主「insping」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hspingcc/article/details/73332526
———————
—————
———
——
———
——
———
——
———
——
———
——
———
——
———
——
———
——
———
——
———
——
———
——————————
java中Date与DateFormat的格式输出
https://blog.csdn.net/moakun/article/details/78638570
__________________________________________________________________________________________
日期相关---SimpleDateFormat的setLenient(true/false)-----自动计算日期
http://www.cnblogs.com/my-king/p/4276577.html
有时候我们需要判断用户的日期格式是否正确,
虽然绝大多数会在前台处理,但是也有需要从文件流读入的情况,如果日期不合格就需要抛异常,这时候就需要禁止SimpleDateFormat的自动计算功能。
这时候就需要用到setLenient,这个方法的含义是是否严格解析日期,具体用法如下。
1 package com.test.date; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 public class TestDate { 7 public static void main(String[] args) { 8 //SimpleDateFormat的setLenient(true) 9 //这种情况下java会把你输入的日期进行计算,比如55个月那么就是4年以后,这时候年份就会变成03年了 10 String dob= "1/55/1999"; 11 Date dateofbirth =null; 12 SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 13 try { 14 dateFormat.setLenient(true); 15 dateofbirth = dateFormat.parse(dob); 16 } catch (Exception e) { 17 e.printStackTrace(); 18 } 19 System.out.println(dateofbirth); 20 //输出是Tue Jul 01 00:00:00 CST 2003 21 22 //SimpleDateFormat的setLenient(false) 23 //这种情况下java不会把你输入的日期进行计算,比如55个月那么就是不合法的日期了,直接异常 24 String dob2= "1/55/1999"; 25 Date dateofbirth2 =null; 26 SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy"); 27 try { 28 dateFormat2.setLenient(false); 29 dateofbirth2 = dateFormat2.parse(dob2); 30 } catch (Exception e) { 31 e.printStackTrace(); 32 } 33 System.out.println(dateofbirth2); 34 /* 35 * 输出如下 36 * java.text.ParseException: Unparseable date: "1/55/1999" 37 at java.text.DateFormat.parse(Unknown Source) 38 at com.test.date.TestDate.main(TestDate.java:28) 39 null 40 */ 41 42 } 43 }
_________________________________________________________________________________________________________________
_____________________________