Documentation

The Java™ Tutorials
Hide TOC
Overview概述
Trail: Date Time
Lesson: Standard Calendar

Overview概述

There are two basic ways to represent time. 存在两种表示时间的基本方法。One way represents time in human terms, referred to as human time, such as year, month, day, hour, minute and second. 一种方式用人类的术语表示时间,称为人类时间,例如年、月、日、时、分和秒。The other way, machine time, measures time continuously along a timeline from an origin, called the epoch, in nanosecond resolution. 另一种方法,机器时间,从一个称为历元的原点沿时间线以纳秒的分辨率连续测量时间。The Date-Time package provides a rich array of classes for representing date and time. 日期时间包提供了一个丰富的类数组来表示日期和时间。Some classes in the Date-Time API are intended to represent machine time, and others are more suited to representing human time.日期时间API中的某些类旨在表示机器时间,而其他类更适合表示人类时间。

First determine what aspects of date and time you require, and then select the class, or classes, that fulfill those needs. 首先确定您需要的日期和时间的哪些方面,然后选择满足这些需求的一个或多个类。When choosing a temporal-based class, you first decide whether you need to represent human time or machine time. 在选择基于时态的类时,首先要确定是需要表示人工时间还是机器时间。You then identify what aspects of time you need to represent. 然后确定需要表示的时间方面。Do you need a time zone? 你需要时区吗?Date and time? Date only? 日期和时间?只有日期?If you need a date, do you need month, day, and year, or a subset?如果需要日期,是需要月、日、年还是子集?


Terminology:术语: The classes in the Date-Time API that capture and work with date or time values, such as Instant, LocalDateTime, and ZonedDateTime, are referred to as temporal-based classes (or types) throughout this tutorial. 在本教程中,日期时间API中捕获并使用日期或时间值(如InstantLocalDateTimeZonedDateTime)的类称为基于时间的类(或类型)。Supporting types, such as the TemporalAdjuster interface or the DayOfWeek enum, are not included in this definition. 此定义中不包括支持类型,例如TemporalAdjuster接口或DayOfWeek枚举。

For example, you might use a LocalDate object to represent a birth date, because most people observe their birthday on the same day, whether they are in their birth city or across the globe on the other side of the international date line. 例如,您可以使用LocalDate对象来表示出生日期,因为大多数人都在同一天庆祝生日,无论他们是在出生城市还是在国际日期线的另一边。If you are tracking astrological time, then you might want to use a LocalDateTime object to represent the date and time of birth, or a ZonedDateTime, which also includes the time zone. 如果您正在跟踪占星术时间,那么您可能希望使用LocalDateTime对象来表示出生日期和时间,或者使用ZonedDateTime,其中也包括时区。If you are creating a time-stamp, then you will most likely want to use an Instant, which allows you to compare one instantaneous point on the timeline to another.如果您正在创建时间戳,那么您很可能希望使用Instant,它允许您将时间线上的一个瞬间点与另一个瞬间点进行比较。

The following table summarizes the temporal-based classes in the java.time package that store date and/or time information, or that can be used to measure an amount of time. 下表总结了java.time包中存储日期和/或时间信息或可用于测量时间量的基于时态的类。A check mark in a column indicates that the class uses that particular type of data and the toString Output column shows an instance printed using the toString method. 列中的复选标记表示类使用该特定类型的数据,而toString输出列显示使用toString方法打印的实例。The Where Discussed column links you to the relevant page in the tutorial.讨论位置列链接到本教程中相关的页面。

Class or Enum类或枚举 Year Month Day Hours Minutes Seconds* Zone Offset Zone ID toString Output Where Discussed讨论位置
Instant          
checked
    2013-08-20T15:16:26.355Z Instant Class
LocalDate
checked
checked
checked
          2013-08-20 Date Classes
LocalDateTime
checked
checked
checked
checked
checked
checked
    2013-08-20T08:16:26.937 Date and Time Classes
ZonedDateTime
checked
checked
checked
checked
checked
checked
checked
checked
2013-08-21T00:16:26.941+09:00[Asia/Tokyo] Time Zone and Offset Classes
LocalTime      
checked
checked
checked
    08:16:26.943 Date and Time Classes
MonthDay  
checked
checked
          --08-20 Date Classes
Year
checked
              2013 Date Classes
YearMonth
checked
checked
            2013-08 Date Classes
Month  
checked
            AUGUST DayOfWeek and Month Enums
OffsetDateTime
checked
checked
checked
checked
checked
checked
checked
  2013-08-20T08:16:26.954-07:00 Time Zone and Offset Classes
OffsetTime      
checked
checked
checked
checked
  08:16:26.957-07:00 Time Zone and Offset Classes
Duration     ** ** **
checked
    PT20H (20 hours) Period and Duration
Period
checked
checked
checked
      *** *** P10D (10 days) Period and Duration
* Seconds are captured to nanosecond precision.秒的捕获精度为纳秒。
** This class does not store this information, but has methods to provide time in these units.此类不存储此信息,但具有以这些单位提供时间的方法。
*** When a Period is added to a ZonedDateTime, daylight saving time or other local time differences are observed.Period添加到ZonedDateTime时,将观察夏令时或其他本地时差。

Previous page: Standard Calendar
Next page: DayOfWeek and Month Enums