新款时间日期API已上线

历史

   早期,Java 对时间和日期的使用每每重度依赖 java.util.Datejava.util.Calendar。 惋惜的是,这2个api 自己不够健壮,有相似线程不安全等诸多问题,因而乎2000年左右,市面上出现了不少用于处理时间类的框架,如Joda-Time。 在Java 8 时代 官方为JDK开发新的时间/日期API的进程:JSR-310,启动了。因而就出现了新一套健壮的API。java

 

Roadmap

新的API

Java 8  中 新的API 包括了 5个包linux

  • java.time数据库

  • java.time.chronoapi

  • java.time.format安全

  • java.time.temporal服务器

  • java.time.zone框架

    java.time 是基础包,里面包含了 时间 日期的 基础类 如  LocalDate, LocalTime等工具

    java.time.chrono  有点像linux 的守护进程 chronyd  用来访问不一样的时间服务器。spa

    java.time.format 顾名思义用来格式化和转换日期时间线程

    java.time.temporal 可能也想不出好名字吧,里面都是些底层扩展类

    java.time.zone 用来对各时区的支持

 

1. 时间线

     Java8中 引入了时间线的概念,时间是接二连三,抽象成一条线,线上的点表示某一时刻。

一班来讲,这个设定主要是面对机器的,不用关心具体的年月日时分秒,内部以秒或者纳秒表示。

1.1 时间点 Instant

      时间点是表示时间的某时某刻的,相对的它是时间线上的一点。

java.time包经过值类型Instant提供机器视图。

Instant表示时间线上的一点,而不须要任何上下文信息(时区,年份)

概念上讲,它只是简单的表示自1970年1月1日0时0分0秒(UTC)开始的秒数。

由于java.time包是基于纳秒计算的,因此Instant的精度能够达到纳秒级。

Instant start = Instant.now()
Instant end = Instant.now()

 

1.2 Period

    Period表示以年、月、日衡量的时长。例如,“3年2个月零6天”。表示的是一段时间 以日月年为单位.

在日期的操做中 能够做为参数来做为日期的加减运算。

Period period = Period.now()

1.3 Duration

    Duration表示以秒和纳秒为基准的时长。例如,“23.6秒”。Duration表示的是一段时间 以 分十秒为单位. 在时间的操做中 能够做为参数 来做为时间的加减运算。

Duration duration = Duration.()System..println(duration)System..println(duration.getSeconds())

 

--------------------------TBD-----------------------

 

2. 不变模式  Immutable

    新的API 提倡采用不变模式,虽然时间是一直在变化的,某事某刻的表示它是固定的,哪怕对日期进行加减,操做以后的日期则表示新的时刻。

   因此Java时间相关的API,在原始时间对象上 作相应的加减操做,原来的日期对象不变,会产生一个新的对象。

3. 本地时间

3.1 本地日期 LocalDate

 LocalDate 是新API 中 最基础也是最重要的类, 它是一个 不可变的类(final 修饰),本地日期只是一个术语,用来表示一个具体日期,不一样地区的日期可能不一样 故采用 local 这个术语,其实它是时区无关的,这个类没有包含任什么时候区信息,就简单的认为一个日期便可。

3.1.1 日期的建立

   日期的建立 采用了工厂方法的模式,全部的建立方法都是私有的,经常使用的是调用对外开放的方法of 来生成一个 日期实例

LocalDate localDate = LocalDate.of(int year, Month month, int dayOfMonth);
LocalDate localDate = LocalDate.of(int year, int month, int dayOfMonth);

 

3.1.2 日期的经常使用方法 - 读

 

    LocalDate 的toString方法 默认返回的日期格式  采用 ISO-8601标准日期  即   YYYY-MM-DD

System.out.println(localDate); //2016-02-18

   LocalDate 还提供了其余不少经常使用方法来得到日期的相关值

System.out.println(localDate.getMonth());       // Month FEBRUARY 当前月
System.out.println(localDate.getMonthValue());  // int  1 当前月的数值

System.out.println(localDate.getDayOfMonth());  // int 18 当前月第X天
System.out.println(localDate.getDayOfWeek());   // DayOfWeek  Monday 当前周的天
System.out.println(localDate.getDayOfYear());   // int 49 当前年的第X天

System.out.println(localDate.getYear());        // int 2016 当前年

System.out.println(localDate.lengthOfYear());   // int 366 当前年共有多少天
System.out.println(localDate.lengthOfMonth());  // int 29  当前月共有多少天

3.1.3 日期的经常使用方法 - 操做

LocalDate 是一个不可变的类,它的每一次操做 都会伴随生成一个新类

localDate.withYear()
localDate.plusMonths()
localDate.minusDays())

3.1.4 日期的经常使用方法 - 属性判断

LocalDate 也提供了一些属性判断的功能

 localDate.isLeapYear(); // true (是闰年)

 

3.2 本地时间 LocalTime

    LocalTime 的用法和 LocalDate 相似一样与与时区无关,  它的默认格式也是采用 ISO-8601 格式为 HH:mm:ss

时间的建立

LocalTime localTime = LocalTime.of(int hour, int minute, int second);
LocalTime localTime = LocalTime.of(int hour, int minute, int second, int nanoOfSecond)
LocalTime localTime = LocalTime.of(int hour, int minute);

例子:

LocalTime localTime = LocalTime.of(17,10,58);

3.3 本地日期时间的组合 LocalDateTime

TBD

 

-------------------------------------Example TBD------------------------------

 

4 时区时间

时区 Zone

5 时间工具

5.1 时间计算器 ChrononUnit

ChronoUnit类可用于在单个时间单位内测量一段时间,例如天数或秒。

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);

5.2 格式转换器 DateTimeFormatter

    Java 8 中 java.time.format 中 围绕  DateTimeFormatter 构建了时间的解析与转换。Java 8 默认的时间显示格式是,ISO 8601标准格式。

5.2.1 使用格式转换器

采用DateTimeFormatter.format() 方法进行格式化输出。

首先须要实例化一个具体对应的时间格式转换器。

如要格式化LocalDate 则须要实例化一个针对LocalDate的转换器

须要注意的是转化器与日期时间对象要一一对应切不可 

LocalDate对象 用DateTimeFormatter.ofLocalDateTime.

这样会抛出UnsupportedTemporalyException.

在实例化一个转换器 须要传递一个格式参数,Java8提供了 预约义的格式和自定义声明

DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);

5.2.1.1 预约格式 FormatStyle

FormatSyle 是一个枚举类,它包括了Full,LONG,MEDIUM,SHORT几种常规的格式。

FormatStyle.FULL  // 'Tuesday, April 12, 1952 AD' or '3:30:42pm PST'.
FormatStyle.LONG // 'January 12, 1952'
FormatStyle.MEDIUM // 'Jan 12, 1952'.
FormatStyle.SHORT //'12.13.52' or '3:30pm'.

5.2.1.2 自定义格式 Pattern

除了以前介绍的一些预约义格式之外,还能够输出一些自定义的时间格式。采用字符串匹配模式

DateTimeFormatter f = DateTimeFormatter.ofPattern("MMMM dd, yyyy, hh:mm");

规则:

通常采用如下规则
 * MMMM 对应月份
   M outputs 1, 
   MM outputs 01, 
   MMM outputs Jan,
   MMMM outputs January.

 * dd,DD 对应天数
 * YYYY,YY,yyyy,yy 对应年份
 * HH 对应 24小时制的 小时
 * hh 对应 12小时制的 小时
 * mm 对应 分钟
 * ss 对应 秒
 * SS 对应 毫秒

此外 还能够包括 相似 / , : - 等分隔符

DateTimeFormatter f1 = DateTimeFormatter.ofPattern("MMMM dd, yyyy, hh:mm");
System.out.println(dateTime.format(f1)); // January 20, 2020, 01:12
DateTimeFormatter f2 = DateTimeFormatter.ofPattern("MM dd, yyyy, hh:mm");
System.out.println(dateTime.format(f2)); // 01 20, 2020, 01:12

DateTimeFormatter f3 = DateTimeFormatter.ofPattern("MMMM DD, YY, HH:mm");
System.out.println(dateTime.format(f3)); // January 20, 2020, 13:12
DateTimeFormatter f4 = DateTimeFormatter.ofPattern("MMMM dd, yyyy, hh:mm");
System.out.println(dateTime.format(f4)); // January 20, 2020, 01:12

DateTimeFormatter f5 = DateTimeFormatter.ofPattern("MMMM dd, yy, hh:mm:ss");
System.out.println(dateTime.format(f5)); // January 20, 2020, 01:12:34
DateTimeFormatter f6 = DateTimeFormatter.ofPattern("MMMM dd, yyyy, hh:mm:ss:SS");
System.out.println(dateTime.format(f6)); // January 20, 2020, 01:12:00

5.2.1.3 预留对象 DateTimeFormatter.IOS***

Java8 预先定义了一些经常使用格式的转换器,能够免去 自定义的麻烦.

DateTimeFormatter 内置了这些对象,能够直接调用。

DateTimeFormatter.ISO_LOCAL_DATE  //'2011-12-03'
DateTimeFormatter.ISO_OFFSET_DATE //'2011-12-03+01:00'
DateTimeFormatter.ISO_DATE        //'2011-12-03' or '2011-12-03+01:00'
DateTimeFormatter.ISO_LOCAL_TIME  //'10:15' or '10:15:30'
DateTimeFormatter.ISO_OFFSET_TIME //'10:15+01:00' or '10:15:30+01:00'
DateTimeFormatter.ISO_TIME        //'10:15', '10:15:30' or '10:15:30+01:00'
DateTimeFormatter.ISO_LOCAL_DATE_TIME //'2011-12-03T10:15:30'
DateTimeFormatter.ISO_OFFSET_DATE_TIME//'2011-12-03T10:15:30+01:00'
DateTimeFormatter.ISO_ZONED_DATE_TIME //'2011-12-03T10:15:30+01:00'

5.2.2 内置Format方法

    时间日期相关类自带一个formate(DateTimeFormatter.TYPE)方法,须要传递一个格式转换器,会根据格式转换器定义的格式作相应的转换。

//建立本地时间
LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
LocalTime time = LocalTime.of(11, 12, 34);
LocalDateTime dateTime = LocalDateTime.of(date, time);

date.format(DateTimeFormatter.ISO_LOCAL_DATE)); //2020-01-20
dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); //2020-01-20T11:12:34
time.format(DateTimeFormatter.ISO_LOCAL_TIME)); //11:12:34

date.format(DateTimeFormatter.ISO_DATE); //2020-01-20
dateTime.format(DateTimeFormatter.ISO_DATE_TIME); //2020-01-20T11:12:34
time.format(DateTimeFormatter.ISO_TIME); //11:12:34

5.3 时间解析器 Parser

 时间的解析和DateTimeFormatter中 ofPatter 结合使用 

配合要解析的字符串对应 时间 就能够了。

DateTimeFormatter f = DateTimeFormatter.ofPattern("MM dd yyyy");
LocalDate date = LocalDate.parse("01 02 2015", f);
LocalTime time = LocalTime.parse("11:22");
System.out.println(date); // 2015-01-02
System.out.println(time); // 11:22

5.4

6 JDBC 与新日期

最新JDBC映射将把数据库的日期类型和Java 8的新类型关联起来:

SQL -> Java
--------------------------
date -> LocalDate
time -> LocalTime
timestamp -> LocalDateTime