java8 - java.time.instant类教程与实例

486 阅读3分钟

instant是一个定义在java.time包中的类。它以正确的纳秒精度在时间轴上表示日期和时间。Java.util.Date以毫秒的精度表示日期和时间。
Instant存储纳秒,所以它需要更多的存储空间,因为它存储的数字比long大。在内部,它存储在两个字段中。作为Instant初始化的一部分,构造函数也有两个字段。
Instant(long epochSecond, int Nanos)
1.Long epochSecond value- 自标准时间以来的秒数
2.整数Nanos值 - 从上一个秒开始的纳秒数。这个值在0到999,999,999之间
它存储更大的长值,而Java.util.Date存储的是自震旦时间以来的若干毫秒的长值。

即时类的签名

public final class Instant  
   extends Object  
      implements Temporal, TemporalAdjuster, Comparable, Serializable  

Instant类是不可变的,并且是线程安全的。

即时类的例子

下面是关于使用方法和如何转换到不同类的例子列表。

instant给出了纳秒级精度的时间,而Date给出了毫秒级精度。instant比Date更精确。我们需要学习如何在java8中把instant转换为Date。from()是java.util.Date类中的一个静态方法,用于从java.time.instant转换为Date对象。

Instant instant=Instant.now();  
    Date date = Date.from(instant);  
    System.out.println(instant);  
    System.out.println(date);  

输出是

2018-08-27T16:04:13.627331100Z  
Mon Aug 27 21:34:13 IST 2018  

Instant是java8中引入的一个新类。在有些情况下,我们需要从Date转换为Instant。Date类不支持纳秒。

Date date = new Date();  
System.out.println(date);  
Instant instant = date.toInstant();  
System.out.println(instant);  

输出为

  
Mon Aug 27 21:39:35 IST 2018  
2018-08-27T16:09:35.429Z  

瞬时类不能使用人类可读的年、月、日单位进行等价检查。为了使相等工作,你需要首先转换为LocalDate或LocalDateTime或ZonedDateTime与即时时区。

Instant instantTime1 = Instant.now();  
Instant instantTime2 = Instant.now().plus(5, ChronoUnit.HOURS);  
Instant instantTime3 = Instant.now().plus(5, ChronoUnit.HOURS);  
LocalDate ld1 = LocalDateTime.ofInstant(instantTime1, ZoneId.systemDefault()).toLocalDate();  
LocalDate ld2 = LocalDateTime.ofInstant(instantTime2, ZoneId.systemDefault()).toLocalDate();  
LocalDate ld3 = LocalDateTime.ofInstant(instantTime3, ZoneId.systemDefault()).toLocalDate();  
System.out.println(ld1.isEqual(ld2));  
System.out.println(ld2.isEqual(ld3));  

LocalDateTime类持有日期、时间和时区信息。使用ZonedDateTime.of()方法通过传递Zone信息和LocalDateTime创建即时对象。

LocalDateTime localDateTime = LocalDateTime.of(2018, Month.AUGUST, 25, 13, 39);  
  Instant instant=ZonedDateTime.of(localDateTime, TimeZone.getTimeZone("EST").toZoneId()).toInstant();  
  System.out.println(instant); 

LocalTime是一个以ISO-8601格式表示没有时区的时间的类。LocalTime和Instant是为了一个不同的目的而引入的。

Instant instant = Instant.now();  
LocalTime local =  LocalTime.from(instant.atZone(ZoneId.of("GMT+3")));  
System.out.println(instant);   
System.out.println(local); 

输出是

2018-08-27T16:30:35.123741700Z  
19:30:35.123741700  

如何在java8中把Instant转换为一个字符串对象?

DateTimeFormatter对象是用格式和默认时区创建的。调用format()方法来转换为字符串。

Instant instant = Instant.now();  
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_INSTANT  
             .withZone(ZoneId.systemDefault());  
     DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")  
             .withZone(ZoneId.systemDefault());  
     DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("MMM dd,yyyy HH:mm:ss")  
             .withZone(ZoneId.systemDefault());  
  System.out.println(instant);   
     System.out.println(dateTimeFormatter.format(instant));  
     System.out.println(dateTimeFormatter1.format(instant));  
     System.out.println(dateTimeFormatter2.format(instant));  

输出是

 2018-08-27T16:42:35.853060600Z  
2018-08-27T16:42:35.853060600Z  
2018-08-27 22:12:35  
Aug 27,2018 22:12:35

如何在java8中添加或减去天、小时、秒的即时对象?

plus()方法用于在添加特定时间后返回新实例。
minus()方法用于在减去特定时间后返回新实例。

 Instant instant = Instant.now();  
System.out.println(instant);  
System.out.println("Add One Day " + instant.plus(Duration.ofDays(1)));  
System.out.println("Add 5 hours " + instant.plus(Duration.ofHours(5)));  
System.out.println("Add 50 minutes" + instant.plus(Duration.ofMinutes(50)));  
System.out.println("Substract One Day " + instant.minus(Duration.ofDays(1)));  
System.out.println("Substract 5 hours " + instant.minus(Duration.ofHours(5)));  
System.out.println("Substrat 50 minutes" + instant.minus(Duration.ofMinutes(50)));

输出是

 2018-08-27T16:51:59.328782200Z  
Add One Day 2018-08-28T16:51:59.328782200Z  
Add 5 hours 2018-08-27T21:51:59.328782200Z  
Add 50 minutes2018-08-27T17:41:59.328782200Z  
Substract One Day 2018-08-26T16:51:59.328782200Z  
Substract 5 hours 2018-08-27T11:51:59.328782200Z  
Substrat 50 minutes2018-08-27T16:01:59.328782200Z

Instant isSupported()方法示例

*isSupported()*方法检查该字段是否被支持。

Instant instant = Instant.now();  
System.out.println(instant);  
System.out.println(instant.isSupported(ChronoUnit.DAYS));    
System.out.println(instant.isSupported(ChronoUnit.YEARS));        
System.out.println(instant.isSupported(ChronoUnit.WEEKS)); 

输出为

2018-08-27T16:57:13.606815900Z  
true  
false  
false