Java 8中时间 API 初探

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

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() 方法会产生一个新的实例,如: google

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


其输出为: url

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


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

//计算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))


其输出结果为: code

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


Localdate 和 LocalTime orm

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)));
相关文章
相关标签/搜索