代码改变世界

java 8新特性 instant

2017-10-14 09:41  晨曦曙光  阅读(43514)  评论(0编辑  收藏  举报

Java 8目前已经开始进入大众的视线,其中笔者在写本文之前,留意到其中Java 8预览版中将会出现新的关于日期和时间的API(遵守JSR 310规范)。在本系列文章中,将对这些新的API进行举例说明。首先在本文中,将先介绍以下几个:Instant,LocalDate,LocalTime 和 LocalDateTime。首先看看 Instant 对象的使用方法: 

Instant (java.time.Instant)

 

可能 java.time 包中最容易开始学习的内容就是 Instant 这个类。所谓的 Instant 类代表的是某个时间(有点像 java.util.Date),它是精确到纳秒的(而不是象旧版本的Date精确到毫秒)。如果使用纳秒去表示一个时间则原来使用一位Long类型是不够的,需要占用更多一点的存储空间,实际上其内部是由两个Long字段组成,第一个部分保存的是自标准Java计算时代(就是1970年1月1日开始)到现在的秒数,第二部分保存的是纳秒数(永远不会超过999,999,999)。我们看下一个具体的例子: 

 

//获得当前时间 
Instant instant = Instant.now();
// 以ISO-8601输出 
System.out.println(instant);

在 Open JDK 中运行上面的代码,可以有如下格式的输出:2013-06-25T16:22:52.966Z,可以看到,输入了更为精确的日期。下面的例子是更多的关于 Instant 类的用法,如: 

//将java.util.Date转换为Instant 
Instant instant = Instant.ofEpochMilli(new Date().getTime()); 
//从字符串类型中创建Instant类型的时间 
instant = Instant.parse("1995-10-23T10:12:35Z");

 

注意,在上面的例子中,有壹個字符串中创建 Instant 类型的时间,但 Instant 代表的是一个时间,并不包括时区的概念,所以必须传入的是符合 UTC格式的字符串。Instant API也提供了一些很有用的方法允许使用Instant 和其他包中的类进行一些运算,下面是例子: 

instant.plus(Duration.ofHours(5).plusMinutes(4));

 

以上的代码表达的含义为,在现在的时间上加上5个小时4分钟。那么这个例子中,使用了多少个 java.time.Instant 实例呢?答案是两个。Java.time 这个包是线程安全的,并且和其他大部分类一样,是不可变类。Instant 也遵守这个规则,因此 plus() 方法会产生一个新的实例,如: 

Instant instant1 = instant.plus(Duration.ofHours(5).plusMinutes(4)); 
System.out.println("Instant is immutable, so instant==instant returns: " + (instant == instant1));

 

其输出为: 

Instant is immutable, so instant==instant returns: false

 

下面是更多的关于计算的例子: 

//计算5天前的时间
instant.minus(5, ChronoUnit.DAYS); // Option 1 方法1 
instant.minus(Duration.ofDays(5)); // Option 2  方法2 
     
//计算两个Instant之间的分钟数 
long diffAsMinutes = instant.periodUntil(instant1, ChronoUnit.MINUTES); // 方法1 
long diffAsMinutes = ChronoUnit.MINUTES.between(instant, instant1); // 方法2

 

Instant 是可比较的,这意味着可以对两个 Instant 进行比较。它提供了 isAfter() 和 isBefore() 两个方法进行比较,如下代码所示: 

//用compareTo方法比较 
System.out.format("instant1.compareTo(instant)=%d.%n", instant1.compareTo(instant)); 
     
// 使用isAfter()和isBefore() 
System.out.format("instant1.isAfter(instant)=%b, instant1.isBefore(instant)=%b.%n",  
instant1.isAfter(instant), instant1.isBefore(instant))

 

其输出结果为: 

instant1.compareTo(instant)=1. 
instant1.isAfter(instant)=true, instant1.isBefore(instant)=false

 

Localdate 和 LocalTime

LocalDate 表示不带时区的日期,比如 1-1-2000。LocalTime 表示不带时区的时间,比如 04:44:50.12,和之前提到的 Instant 类是从1970年计算偏移量不同,这两个类的输出是人们习惯阅读的日期和时间。有很多种方法去获得 LocalTime 和 LocalDate 的实例,如: 

LocalDate localDate = LocalDate.now(); 
localDate = LocalDate.ofYearDay(2005, 86); // 获得2005年的第86天 (27-Mar-2005) 
localDate = LocalDate.of(2013, Month.AUGUST, 10); //2013年8月10日 
LocalTime localTime = LocalTime.of(22, 33); //10:33 PM 
localTime = LocalTime.now(); 
localTime = LocalTime.ofSecondOfDay(4503); // 返回一天中的第4503秒 (1:15:30 AM)

 

LocalDate 和 LocalTime 和 Instant 一样遵守同样的线程规定―― 如它们的实例子都是不可变的。LocalDate 和 LocalTime 和 Instant 有同样的计算和比较方法(其中有些方法是在 java.time.temporal.Temporal接口中定义的,并且上面这些类都实现了这些方法): 

LocalDate localDate1 = localDate.plus(5, ChronoUnit.HOURS); 
localDate.isBefore(localDate1);

 

LocalDateTime

最后来看下在简单日期和时间类中最重要的一个:LocalDataTeime。它是LocalDate和LocalTime的组合体,表示的是不带时区的 日期及时间。看上去,LocalDateTime和Instant很象,但记得的是“Instant中是不带时区的即时时间点。可能有人说,即时的时间点 不就是日期+时间么?看上去是这样的,但还是有所区别,比如LocalDateTime对于用户来说,可能就只是一个简单的日期和时间的概念,考虑如下的 例子:两个人都在2013年7月2日11点出生,第一个人是在英国出生,而第二个是在加尼福利亚,如果我们问他们是在什么时候出生的话,则他们看上去都是 在同样的时间出生(就是LocalDateTime所表达的),但如果我们根据时间线(如格林威治时间线)去仔细考察,则会发现在出生的人会比在英国出生的人稍微晚几个小时(这就是Instant所表达的概念,并且要将其转换为UTC格式的时间)。除此之外,LocalDateTime 的用法和上述介绍的其他类都很相似,如下例子所示: 

LocalDateTime localDateTime = LocalDateTime.now(); 
//当前时间加上25小时3分钟 
LocalDateTime inTheFuture = localDateTime.plusHours(25).plusMinutes(3); 
// 同样也可以用在localTime和localDate中 
System.out.println(localDateTime.toLocalTime().plusHours(25).plusMinutes(3)); 
System.out.println(localDateTime.toLocalDate().plusMonths(2)); 
// 也可以使用实现TemportalAmount接口的Duration类和Period类 
System.out.println(localDateTime.toLocalTime().plus(Duration.ofHours(25).plusMinutes(3))); 
System.out.println(localDateTime.toLocalDate().plus(Period.ofMonths(2)));