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发行说明。
The LocalTime class is similar to the other classes whose names are prefixed with Local, but deals in time only. LocalTime类类似于名称前缀为Local的其他类,但只处理时间。This class is useful for representing human-based time of day, such as movie times, or the opening and closing times of the local library. 此类用于表示基于人的时间,例如电影时间或本地库的打开和关闭时间。It could also be used to create a digital clock, as shown in the following example:它还可用于创建数字时钟,如以下示例所示:
LocalTime thisSec; for (;;) { thisSec = LocalTime.now(); // implementation of display code is left to the reader display(thisSec.getHour(), thisSec.getMinute(), thisSec.getSecond()); }
The LocalTime class does not store time zone or daylight saving time information.LocalTime类不存储时区或夏令时信息。
The class that handles both date and time, without a time zone, is LocalDateTime, one of the core classes of the Date-Time API. 处理日期和时间的类(没有时区)是LocalDateTime,它是日期时间API的核心类之一。This class is used to represent date (month-day-year) together with time (hour-minute-second-nanosecond) and is, in effect, a combination of LocalDate with LocalTime. 此类用于表示日期(月-日-年)和时间(小时-分钟-秒-纳秒),实际上是LocalDate和LocalTime的组合。This class can be used to represent a specific event, such as the first race for the Louis Vuitton Cup Finals in the America's Cup Challenger Series, which began at 1:10 p.m. on August 17, 2013. 这个类可以用来代表一个特定的事件,例如美国杯挑战者系列赛中路易威登杯决赛的第一场比赛,比赛在2013年8月17日下午1:10开始。Note that this means 1:10 p.m. in local time. 请注意,这意味着当地时间下午1:10。To include a time zone, you must use a ZonedDateTime or an OffsetDateTime, as discussed in Time Zone and Offset Classes.要包含时区,必须使用ZonedDateTime或OffsetDateTime,如时区和偏移类中所述。
In addition to the now method that every temporal-based class provides, the LocalDateTime class has various of methods (or methods prefixed with of) that create an instance of LocalDateTime. 除了每个基于时态的类提供的now方法之外,LocalDateTime类还具有各种方法(或以of为前缀的方法),用于创建LocalDateTime实例。There is a from method that converts an instance from another temporal format to a LocalDateTime instance. 有一个from方法将实例从另一种时态格式转换为LocalDateTime实例。There are also methods for adding or subtracting hours, minutes, days, weeks, and months. 还有添加或减去小时、分钟、天、周和月的方法。The following example shows a few of these methods. 下面的示例显示了其中的一些方法。The date-time expressions are in bold:日期时间表达式以粗体显示:
System.out.printf("now: %s%n", LocalDateTime.now()); System.out.printf("Apr 15, 1994 @ 11:30am: %s%n", LocalDateTime.of(1994, Month.APRIL, 15, 11, 30)); System.out.printf("now (from Instant): %s%n", LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())); System.out.printf("6 months from now: %s%n", LocalDateTime.now().plusMonths(6)); System.out.printf("6 months ago: %s%n", LocalDateTime.now().minusMonths(6));
This code produces output that will look similar to the following:此代码生成的输出类似于以下内容:
now: 2013-07-24T17:13:59.985 Apr 15, 1994 @ 11:30am: 1994-04-15T11:30 now (from Instant): 2013-07-24T17:14:00.479 6 months from now: 2014-01-24T17:14:00.480 6 months ago: 2013-01-24T17:14:00.481