Documentation

The Java™ Tutorials
Hide TOC
Date ClassesDate类
Trail: Date Time
Lesson: Standard Calendar标准日历

Date ClassesDate类

The Date-Time API provides four classes that deal exclusively with date information, without respect to time or time zone. Date-Time API提供了四个类,专门处理日期信息,而不考虑时间或时区。The use of these classes are suggested by the class names: LocalDate, YearMonth, MonthDay, and Year.通过类名建议使用这些类:LocalDateYearMonthMonthDayYear

LocalDate

A LocalDate represents a year-month-day in the ISO calendar and is useful for representing a date without a time. LocalDate表示ISO日历中的年-月-日,用于表示没有时间的日期。You might use a LocalDate to track a significant event, such as a birth date or wedding date. 您可以使用LocalDate跟踪重要事件,例如出生日期或结婚日期。The following examples use the of and with methods to create instances of LocalDate:以下示例使用ofwith方法创建LocalDate的实例:

LocalDate date = LocalDate.of(2000, Month.NOVEMBER, 20);
LocalDate nextWed = date.with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));

For more information about the TemporalAdjuster interface, see Temporal Adjuster.有关TemporalAdjuster接口的详细信息,请参阅临时调整器

In addition to the usual methods, the LocalDate class offers getter methods for obtaining information about a given date. 除了常用的方法外,LocalDate类还提供获取给定日期信息的getter方法。The getDayOfWeek method returns the day of the week that a particular date falls on. getDayOfWeek方法返回特定日期所在的星期几。For example, the following line of code returns "MONDAY":例如,以下代码行返回“星期一”:

DayOfWeek dotw = LocalDate.of(2012, Month.JULY, 9).getDayOfWeek();

The following example uses a TemporalAdjuster to retrieve the first Wednesday after a specific date.以下示例使用TemporalAdjuster检索特定日期后的第一个星期三。

LocalDate date = LocalDate.of(2000, Month.NOVEMBER, 20);
TemporalAdjuster adj = TemporalAdjusters.next(DayOfWeek.WEDNESDAY);
LocalDate nextWed = date.with(adj);
System.out.printf("For the date of %s, the next Wednesday is %s.%n",
                  date, nextWed);

Running the code produces the following:运行代码会产生以下结果:

For the date of 2000-11-20, the next Wednesday is 2000-11-22.

The Period and Duration section also has examples using the LocalDate class.Period和Duration部分还有使用LocalDate类的示例。

YearMonth

The YearMonth class represents the month of a specific year. YearMonth类表示特定年份的月份。The following example uses the YearMonth.lengthOfMonth() method to determine the number of days for several year and month combinations.下面的示例使用YearMonth.lengthOfMonth()方法确定若干年和月组合的天数。

YearMonth date = YearMonth.now();
System.out.printf("%s: %d%n", date, date.lengthOfMonth());

YearMonth date2 = YearMonth.of(2010, Month.FEBRUARY);
System.out.printf("%s: %d%n", date2, date2.lengthOfMonth());

YearMonth date3 = YearMonth.of(2012, Month.FEBRUARY);
System.out.printf("%s: %d%n", date3, date3.lengthOfMonth());

The output from this code looks like the following:此代码的输出如下所示:

2013-06: 30
2010-02: 28
2012-02: 29

MonthDay

The MonthDay class represents the day of a particular month, such as New Year's Day on January 1.MonthDay类表示特定月份的某一天,例如元旦1月1日。

The following example uses the MonthDay.isValidYear method to determine if February 29 is valid for the year 2010. 以下示例使用MonthDay.isValidYear方法确定2月29日是否对2010年有效。The call returns false, confirming that 2010 is not a leap year.该调用返回false,确认2010年不是闰年。

MonthDay date = MonthDay.of(Month.FEBRUARY, 29);
boolean validLeapYear = date.isValidYear(2010);

Year

The Year class represents a year. Year类代表一年。The following example uses the Year.isLeap method to determine if the given year is a leap year. 以下示例使用Year.isLeap方法确定给定年份是否为闰年。The call returns true, confirming that 2012 is a leap year.该调用返回true,确认2012年是闰年。

boolean validLeapYear = Year.of(2012).isLeap();

Previous page: DayOfWeek and Month Enums
Next page: Date and Time Classes