The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
One of the core classes of the Date-Time API is the Instant class, which represents the start of a nanosecond on the timeline. Date-Time API的核心类之一是Instant类,它表示时间线上一纳秒的开始。This class is useful for generating a time stamp to represent machine time.此类用于生成表示机器时间的时间戳。
import java.time.Instant; Instant timestamp = Instant.now();
A value returned from the Instant class counts time beginning from the first second of January 1, 1970 (1970-01-01T00:00:00Z) also called the EPOCH. Instant类返回的值计算从1970年1月1日(1970-01-01T00:00:00Z)的第一秒开始的时间,也称为历元。An instant that occurs before the epoch has a negative value, and an instant that occurs after the epoch has a positive value.在历元之前发生的瞬间具有负值,而在历元之后发生的瞬间则具有正值。
The other constants provided by the Instant class are MIN, representing the smallest possible (far past) instant, and MAX, representing the largest (far future) instant.Instant类提供的其他常数是MIN,表示最小可能(远过去)的瞬间,MAX,表示最大(远未来)的瞬间。
Invoking toString on an Instant produces output like the following:在Instant上调用toString会产生如下输出:
2013-05-30T23:38:23.085Z
This format follows the ISO-8601 standard for representing date and time.该格式遵循ISO-8601标准表示日期和时间。
The Instant class provides a variety of methods for manipulating an Instant. Instant类提供了多种方法来操作Instant。There are plus and minus methods for adding or subtracting time. The following code adds 1 hour to the current time:有plus和minus时间的方法。以下代码将当前时间增加1小时:
Instant oneHourLater = Instant.now().plus(1, ChronoUnit.HOURS);
There are methods for comparing instants, such as isAfter and isBefore. 有比较实例的方法,例如isAfter和isBefore。The until method returns how much time exists between two Instant objects. until方法返回两个Instant对象之间存在的时间。The following line of code reports how many seconds have occurred since the beginning of the Java epoch.下面的代码行报告了自Java纪元开始以来发生了多少秒。
long secondsFromEpoch = Instant.ofEpochSecond(0L).until(Instant.now(), ChronoUnit.SECONDS);
The Instant class does not work with human units of time, such as years, months, or days. Instant类不使用人类时间单位,如年、月或日。If you want to perform calculations in those units, you can convert an Instant to another class, such as LocalDateTime or ZonedDateTime, by binding the Instant with a time zone. 如果要以这些单位执行计算,可以通过将Instant与时区绑定,将Instant转换为其他类,如LocalDateTime或ZonedDateTime。You can then access the value in the desired units. 然后,您可以访问所需单位的值。The following code converts an Instant to a LocalDateTime object using the ofInstant method and the default time zone, and then prints out the date and time in a more readable form:以下代码使用ofInstant方法和默认时区将Instant转换为LocalDateTime对象,然后以更可读的形式打印日期和时间:
Instant timestamp; ... LocalDateTime ldt = LocalDateTime.ofInstant(timestamp, ZoneId.systemDefault()); System.out.printf("%s %d %d at %d:%d%n", ldt.getMonth(), ldt.getDayOfMonth(), ldt.getYear(), ldt.getHour(), ldt.getMinute());
The output will be similar to the following:输出将类似于以下内容:
MAY 30 2013 at 18:21
Either a ZonedDateTime or an OffsetTimeZone object can be converted to an Instant object, as each maps to an exact moment on the timeline. ZonedDateTime或OffsetTimeZone对象都可以转换为即时对象,因为每个对象都映射到时间线上的精确时刻。However, the reverse is not true. 然而,事实并非如此。To convert an Instant object to a ZonedDateTime or an OffsetDateTime object requires supplying time zone, or time zone offset, information.要将Instant对象转换为ZonedDateTime或OffsetDateTime对象,需要提供时区或时区偏移信息。