为什么Java8中需要导入新的日期库与时间库

  介绍

为什么Java8中需要导入新的日期库与时间库?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1,日期日期输出可读性较差

日期日期=new日期();
  System.out.println(日期);

打印输出的结果:

坐2020年11月14日11:03:41 CST

2,日期日期的解析,格式化通过JDK自带的api实现较为麻烦,通常会使用第三方的日期时间库,比如:joda-time, commons lang

<强> Java8中提供了哪些日期和时间类

在java。时间包中提供了很多新的类,通常主要使用到的是LocalDate,本地时间,LocalDateTime, ZoneId, ZoneDateTime;关系图如下:

为什么Java8中需要导入新的日期库与时间库

LocaDate这个类本身不包含时间和时区信息,只包含了日期信息;提供了很多方法来获取常用的值:星期几,几月…

常用的静态构造LocaDate方法

LocalDate。(2020、11、14);//指定年月日   LocalDate。(2020年,月。11月14日);//指定年月日使用月枚举类   LocalDate。10 ofYearDay (2020);//2020年第十天=比;2020-01-10   LocalDate.now ();//当前时间   System.out.println (LocalDate.now ());//比较好的可读性输出=比;2020-11-14

LocaDate常用实例方法

本地时间。(12日9、10);//时、分、秒
  LocalTime.now ();
  作用时间=作用。(12日9、10);
  System.out.println (time.getHour ());
  System.out.println (time.getMinute ());
  System.out.println (time.getSecond ()); 

LocalDateTime从这个类的名字可以看出是合并了LocalDate,本地时间,只包含日期和时间,不包含时区信息

构造的方式,可以直接使用静态方法创建,也可以通过LocalDate,作用是合并

LocalDateTime.of (LocalDate.now (), LocalTime.now ());   LocalDateTime。(10 2020、11、14日,13日,50);   LocalDate.now () .atTime (LocalTime.now ());   LocalTime.now () .atDate (LocalDate.now ());   LocalDateTime.now ();

由于LocalDateTime是LocalDate,本地时间的合并,所以LocalDate,作用是有的实例方法,基本在LocalDateTime中都可以找到

ZoneId用来替代老版本时区,每个ZoneId都有一个特定的地区标识;

ZoneId.of(“亚洲/Shanghai");   ZoneId.systemDefault ()

查看所有的地区标识可以进入到ZoneId源码

ZoneDateTime带有日期,时间,时区信息,是LocalDateTime和ZoneId的组合

 ZonedDateTime ZonedDateTime=ZonedDateTime.of (LocalDateTime.now (), ZoneId.systemDefault ());
  LocalTime.now ZonedDateTime.of (LocalDate.now () (), ZoneId.of(“亚洲/Shanghai")); 

经常我们会遇到需要求两个时间之间相差的时间,如何实现呢?

<强> Java8也提供给了相应的API的支持,持续时间、周期

=Duration.between(本地时间之间的时间。0(13日)作用。0)(14日);
  between.getSeconds ();//返回两个时间相差的秒数=比;3600年

时间是通过秒和毫秒来记录时间的长短,所以只能处理两个作用是,DateLocalTime, ZonedDateTime;如果传入的是LocalDate,将会抛出异常

java.time.temporal。UnsupportedTemporalTypeException:不支持单位:秒      java.time.LocalDate.until (LocalDate.java: 1614)   java.time.Duration.between (Duration.java: 475)   com.haixue.crm.stock.service.LocalTest.testDate (LocalTest.java: 121)   在sun.reflect.NativeMethodAccessorImpl。invoke0(本地方法)   sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java: 62)   sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java: 43)   java.lang.reflect.Method.invoke (Method.java: 498)   org.junit.runners.model.FrameworkMethod runreflectivecall 1.美元(FrameworkMethod.java: 50)   org.junit.internal.runners.model.ReflectiveCallable.run (ReflectiveCallable.java: 12)   在org.junit.runners.model.FrameworkMethod.invokeExplosively (FrameworkMethod.java: 47)   在org.junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod.java: 17)   org.junit.runners.ParentRunner.runLeaf (ParentRunner.java: 325)   org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 78)   在org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 57)   在org.junit.runners.ParentRunner 3.美元运行(ParentRunner.java: 290)   在org.junit.runners.ParentRunner 1.美元计划(ParentRunner.java: 71)   org.junit.runners.ParentRunner.runChildren (ParentRunner.java: 288)   org.junit.runners.ParentRunner.access 000美元(ParentRunner.java: 58)   org.junit.runners.ParentRunner评估2.美元(ParentRunner.java: 268)   org.junit.runners.ParentRunner.run (ParentRunner.java: 363)   org.junit.runner.JUnitCore.run (JUnitCore.java: 137)   com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs (JUnit4IdeaTestRunner.java: 68)   com.intellij.rt.execution.junit.IdeaTestRunner Repeater.startRunnerWithArgs美元(IdeaTestRunner.java: 47)   com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart (JUnitStarter.java: 242)   com.intellij.rt.execution.junit.JUnitStarter.main (JUnitStarter.java: 70)

为什么Java8中需要导入新的日期库与时间库