Accessing Fields in Dates and Times
A temporal object provides get methods that are tailored to access the values of specific temporal fields that constitute its state. The LocalTime and LocalDate classes provide get methods to access the values of time and date fields, respectively. Not surprisingly, the LocalDateTime class provides get methods for accessing the values of both time and date fields.
// LocalTime, LocalDateTime
int getHour()
int getMinute()
int getSecond()
int getNano()
Return the value of the appropriate time field from the current LocalTime or LocalDateTime object.
// LocalDate, LocalDateTime
int getDayOfMonth()
DayOfWeek getDayOfWeek()
int getDayOfYear()
Month getMonth()
int getMonthValue()
int getYear()
Return the value of the appropriate date field from the current LocalDate or LocalDateTime object. The enum type DayOfWeek allows days of the week to be referred to by name; for example, DayOfWeek.MONDAY is day 1 of the week. The enum type Month allows months of the year to be referred to by nameāfor example, Month.JANUARY. The month value is from 1 (Month.JANUARY) to 12 (Month.DECEMBER).
// LocalTime, LocalDate, LocalDateTime
int get(TemporalField field)
long getLong(TemporalField field)
boolean isSupported(TemporalField field)
The first two methods return the value of the specified TemporalField (p. 1046) from this temporal object as an int value or as a long value, respectively. To specify fields whose value does not fit into an int, the getLong() method must be used.
The third method checks if the specified field is supported by this temporal object. It avoids an exception being thrown if it has been determined that the field is supported.
Using an invalid field in a get method will result in any one of these exceptions: DateTimeException (field value cannot be obtained), UnsupportedTemporalType-Exception (field is not supported), or ArithmeticException (numeric overflow occurred).
Here are some examples of using the get methods; more examples can be found in Example 17.2. Given that time and date refer to a LocalTime (08:15) and a LocalDate (1945-08-06), respectively, the code below shows how we access the values of the temporal fields using specifically named get methods and using specific temporal fields.
int minuteOfHour1 = time.getMinute(); // 15
int minuteOfHour2 = time.get(ChronoField.MINUTE_OF_HOUR); // 15
int monthVal1 = date.getMonthValue(); // 8
int monthVal2 = date.get(ChronoField.MONTH_OF_YEAR); // 8
The temporal class LocalDateTime also provides two methods to obtain the date and the time as temporal objects, in contrast to accessing the values of individual date and time fields.
LocalDateTime doomsday = LocalDateTime.of(1945, 8, 6, 8, 15);
LocalDate date = doomsday.toLocalDate(); // 1945-08-06
LocalTime time = doomsday.toLocalTime(); // 08:15
// LocalDateTime
LocalDate toLocalDate()
LocalTime toLocalTime()
These methods can be used to get the LocalDate and the LocalTime components of this date-time object, respectively.
The following two methods return the number of days in the month and in the year represented by a LocalDate object.
LocalDate foolsday = LocalDate.of(2022, 4, 1);
int daysInMonth = foolsday.lengthOfMonth(); // 30
int daysInYear = foolsday.lengthOfYear(); // 365 (2022 is not a leap year.)
// LocalDate
int lengthOfMonth()
int lengthOfYear()
These two methods return the number of days in the month and in the year represented by this date, respectively.