Java Reference
In-Depth Information
JULY, FEBRUARY, MARCH, JULY, 2
Representing the Day of Week
The DayOfWeek enum has seven constants to represent seven days of the week. The constants are MONDAY , TUESDAY ,
WEDNESDAY , THURSDAY , FRIDAY , SATURDAY , and SUNDAY . Its getValue() method returns an int value: 1 for Monday, 2 for
Tuesday, and so on, which follows ISO-8601 standards. The DayOfWeek enum is in the java.time package. Below are
some examples of using the DayOfWeek enum and its methods.
LocalDate ld = LocalDate.of(2012, 5, 10);
// Extract the day-of-week from a LocalDate
DayOfWeek dw1 = DayOfWeek.from(ld); // THURSDAY
// Get the int value of the day-of-week
int dw11 = dw1.getValue(); // 4
// Use the method of the LocalDate class to get day-of-week
DayOfWeek dw12 = ld.getDayOfWeek(); // THURSDAY
// Obtain a DayOfWeek instance using an int value
DayOfWeek dw2 = DayOfWeek.of(7); // SUNDAY
// Add one day to the day-of-week to get the next day
DayOfWeek dw3 = dw2.plus(1); // MONDAY
// Get the day-of-week two days ago
DayOfWeek dw4 = dw2.minus(2); // FRIDAY
Representing DateTime Fields
Most fields in datetime can be represented as a numeric value, for example, year, month, day, hour, etc. An instance
of the TemporalField interface represents a field of datetime, for example, year, month-of-year, minutes-of-hour, etc.
The ChronoField enum implements the TemporalField interface and provides several constants to represent fields
in a datetime. The ChronoField enum contains a long list of constants. Some of them are as follows: AMPM_OF_DAY,
CLOCK_HOUR_OF_AMPM, CLOCK_HOUR_OF_DAY, DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_YEAR, ERA, HOUR_OF_AMPM,
HOUR_OF_DAY, INSTANT_SECONDS, MINUTE_OF_HOUR, MONTH_OF_YEAR, SECOND_OF_MINUTE, YEAR, YEAR_OF_ERA.
Typically, you use a TemporalField to get the value of the field from a datetime. All datetime classes have a get()
method that returns an int value for the specified TemporalField . If the value for a field is too long for an int , use the
companion getLong() method to get the value in a long .
Not all datetime classes support all type of fields. For example, a LocalDate does not support the MINUTE_OF_HOUR
field. Use the isSupported() method of the datetime classes to check whether they support a specific type of field.
Use the isSupportedBy() method of ChronoField to check if a field is supported by a datetime class.
Search WWH ::




Custom Search