您的位置:首页 > 编程语言 > Java开发

新款时间日期API已上线

2016-01-18 00:00 561 查看
摘要: http://www.blogjava.net/4cai/archive/2014/04/29/413041.html http://docs.oracle.com/javase/8/docs/api/

历史

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

Roadmap



新的API

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

java.time

java.time.chrono

java.time.format

java.time.temporal

java.time.zone

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

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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息