In this article, we will discuss what are all the Temporal Fields supported by LocalDate using isSupported() method provided in Java 1.8 version
1. LocalDate & TemporalField :
- A field of date-time, such as month-of-year or hour-of-minute
- Date and time is expressed using fields which partition the time-line into something meaningful for humans
- Implementations of this interface represent those fields
- isSupported(TemporalField) – checks if the specified field is supported by invoking LocalDate
- Return true, if supported
- Returns false, if not supported
- There are various methods available in LocalDate which can be used after checking if they are supported by LocalTime
- get(TemporalField) – gets the value of the specified field from invoking LocalDate as an int
- getLong(TemporalField) – gets the value of the specified field from invoking LocalDate as a long
- range(TemporalField) – gets the range of valid values for the specified field
- with(TemporalField, long) – returns a copy of invoking LocalDate with the specified field set to a new value
- Below are the list of Temporal Fields (ChronoField) supported by invoking LocalDate
- DAY_OF_WEEK
- ALIGNED_DAY_OF_WEEK_IN_MONTH
- ALIGNED_DAY_OF_WEEK_IN_YEAR
- DAY_OF_MONTH
- DAY_OF_YEAR
- EPOCH_DAY
- ALIGNED_WEEK_OF_MONTH
- ALIGNED_WEEK_OF_YEAR
- MONTH_OF_YEAR
- PROLEPTIC_MONTH
- YEAR_OF_ERA
- YEAR
- ERA
- Below are the list of Temporal Fields (ChronoField) NOT supported by invoking LocalDate
- NANO_OF_SECOND
- NANO_OF_DAY
- MICRO_OF_SECOND
- MICRO_OF_DAY
- MILLI_OF_SECOND
- MILLI_OF_DAY
- SECOND_OF_MINUTE
- SECOND_OF_DAY
- MINUTE_OF_HOUR
- MINUTE_OF_DAY
- HOUR_OF_AMPM
- CLOCK_HOUR_OF_AMPM
- HOUR_OF_DAY
- CLOCK_HOUR_OF_DAY
- AMPM_OF_DAY
- INSTANT_SECONDS
- OFFSET_SECONDS
- There are 30 enum constants in ChronoField out of which 13 are supported by LocalDate
- Lets see 2 examples covering each of the above ChronoField ENUM constants
2. Examples on LocalDate & TemporalField :
- Temporal Fields supported by LocalDate
- Temporal Fields not supported by LocalDate
2.1 Temporal Fields supported by LocalDate :
- This example covers all the Temporal Fields supported by LocalDate
CheckLocalDateIsSupportedUsingTemporalField.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
public class CheckLocalDateIsSupportedUsingTemporalField {
public static void main(String[] args) {
// get current system date
LocalDate localDate = LocalDate.now();
System.out.println("Current system date is = \n" + localDate);
// •DAY_OF_WEEK
// •ALIGNED_DAY_OF_WEEK_IN_MONTH
// •ALIGNED_DAY_OF_WEEK_IN_YEAR
// •DAY_OF_MONTH
// •DAY_OF_YEAR
// •EPOCH_DAY
// •ALIGNED_WEEK_OF_MONTH
// •ALIGNED_WEEK_OF_YEAR
// •MONTH_OF_YEAR
// •PROLEPTIC_MONTH
// •YEAR_OF_ERA
// •YEAR
// •ERA
// Except above, all other ChronoField instances will return false
// 1. check ChronoField.DAY_OF_WEEK field supported ?
System.out.println("\n1. LocalDate.isSupported(ChronoField.DAY_OF_WEEK) ? " +
localDate.isSupported(ChronoField.DAY_OF_WEEK));
// 2. check ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH field supported ?
System.out.println("\n2. LocalDate.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH) ? " +
localDate.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH));
// 3. check ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR field supported ?
System.out.println("\n3. LocalDate.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR) ? " +
localDate.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR));
// 4. check ChronoField.DAY_OF_MONTH field supported ?
System.out.println("\n4. LocalDate.isSupported(ChronoField.DAY_OF_MONTH) ? " +
localDate.isSupported(ChronoField.DAY_OF_MONTH));
// 5. check ChronoField.DAY_OF_YEAR field supported ?
System.out.println("\n5. LocalDate.isSupported(ChronoField.DAY_OF_YEAR) ? " +
localDate.isSupported(ChronoField.DAY_OF_YEAR));
// 6. check ChronoField.EPOCH_DAY field supported ?
System.out.println("\n6. LocalDate.isSupported(ChronoField.EPOCH_DAY) ? " +
localDate.isSupported(ChronoField.EPOCH_DAY));
// 7. check ChronoField.ALIGNED_WEEK_OF_MONTH field supported ?
System.out.println("\n7. LocalDate.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH) ? " +
localDate.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH));
// 8. check ChronoField.ALIGNED_WEEK_OF_YEAR field supported ?
System.out.println("\n8. LocalDate.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR) ? " +
localDate.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR));
// 9. check ChronoField.MONTH_OF_YEAR field supported ?
System.out.println("\n9. LocalDate.isSupported(ChronoField.MONTH_OF_YEAR) ? " +
localDate.isSupported(ChronoField.MONTH_OF_YEAR));
// 10. check ChronoField.PROLEPTIC_MONTH field supported ?
System.out.println("\n10. LocalDate.isSupported(ChronoField.PROLEPTIC_MONTH) ? " +
localDate.isSupported(ChronoField.PROLEPTIC_MONTH));
// 11. check ChronoField.YEAR_OF_ERA field supported ?
System.out.println("\n11. LocalDate.isSupported(ChronoField.YEAR_OF_ERA) ? " +
localDate.isSupported(ChronoField.YEAR_OF_ERA));
// 12. check ChronoField.YEAR field supported ?
System.out.println("\n12. LocalDate.isSupported(ChronoField.YEAR) ? " +
localDate.isSupported(ChronoField.YEAR));
// 13. check ChronoField.ERA field supported ?
System.out.print("\n13. LocalDate.isSupported(ChronoField.ERA) ? " +
localDate.isSupported(ChronoField.ERA));
}
}
Output:
Current system date is =
2022-08-07
1. LocalDate.isSupported(ChronoField.DAY_OF_WEEK) ? true
2. LocalDate.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH) ? true
3. LocalDate.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR) ? true
4. LocalDate.isSupported(ChronoField.DAY_OF_MONTH) ? true
5. LocalDate.isSupported(ChronoField.DAY_OF_YEAR) ? true
6. LocalDate.isSupported(ChronoField.EPOCH_DAY) ? true
7. LocalDate.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH) ? true
8. LocalDate.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR) ? true
9. LocalDate.isSupported(ChronoField.MONTH_OF_YEAR) ? true
10. LocalDate.isSupported(ChronoField.PROLEPTIC_MONTH) ? true
11. LocalDate.isSupported(ChronoField.YEAR_OF_ERA) ? true
12. LocalDate.isSupported(ChronoField.YEAR) ? true
13. LocalDate.isSupported(ChronoField.ERA) ? true
2.2 Temporal Fields not supported by LocalDate :
- This example covers Temporal Fields not supported by LocalDate
CheckLocalDateIsSupportedUsingTemporalField2.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
public class CheckLocalDateIsSupportedUsingTemporalField2 {
public static void main(String[] args) {
// get current system date
LocalDate localDate = LocalDate.now();
System.out.println("Current system date is = \n" + localDate);
// •NANO_OF_SECOND
// •NANO_OF_DAY
// •MICRO_OF_SECOND
// •MICRO_OF_DAY
// •MILLI_OF_SECOND
// •MILLI_OF_DAY
// •SECOND_OF_MINUTE
// •SECOND_OF_DAY
// •MINUTE_OF_HOUR
// •MINUTE_OF_DAY
// •HOUR_OF_AMPM
// •CLOCK_HOUR_OF_AMPM
// •HOUR_OF_DAY
// •CLOCK_HOUR_OF_DAY
// •AMPM_OF_DAY
// •INSTANT_SECONDS
// •OFFSET_SECONDS
// Above ChronoField instances will return false
// 1. check ChronoField.NANO_OF_SECOND field supported ?
System.out.println("\n1. LocalDate.isSupported(ChronoField.NANO_OF_SECOND) ? " +
localDate.isSupported(ChronoField.NANO_OF_SECOND));
// 2. check ChronoField.NANO_OF_DAY field supported ?
System.out.println("\n2. LocalDate.isSupported(ChronoField.NANO_OF_DAY) ? " +
localDate.isSupported(ChronoField.NANO_OF_DAY));
// 3. check ChronoField.MICRO_OF_SECOND field supported ?
System.out.println("\n3. LocalDate.isSupported(ChronoField.MICRO_OF_SECOND) ? " +
localDate.isSupported(ChronoField.MICRO_OF_SECOND));
// 4. check ChronoField.MICRO_OF_DAY field supported ?
System.out.println("\n4. LocalDate.isSupported(ChronoField.MICRO_OF_DAY) ? " +
localDate.isSupported(ChronoField.MICRO_OF_DAY));
// 5. check ChronoField.MILLI_OF_SECOND field supported ?
System.out.println("\n5. LocalDate.isSupported(ChronoField.MILLI_OF_SECOND) ? " +
localDate.isSupported(ChronoField.MILLI_OF_SECOND));
// 6. check ChronoField.MILLI_OF_DAY field supported ?
System.out.println("\n6. LocalDate.isSupported(ChronoField.MILLI_OF_DAY) ? " +
localDate.isSupported(ChronoField.MILLI_OF_DAY));
// 7. check ChronoField.SECOND_OF_MINUTE field supported ?
System.out.println("\n7. LocalDate.isSupported(ChronoField.SECOND_OF_MINUTE) ? " +
localDate.isSupported(ChronoField.SECOND_OF_MINUTE));
// 8. check ChronoField.SECOND_OF_DAY field supported ?
System.out.println("\n8. LocalDate.isSupported(ChronoField.SECOND_OF_DAY) ? " +
localDate.isSupported(ChronoField.SECOND_OF_DAY));
// 9. check ChronoField.MINUTE_OF_HOUR field supported ?
System.out.println("\n9. LocalDate.isSupported(ChronoField.MINUTE_OF_HOUR) ? " +
localDate.isSupported(ChronoField.MINUTE_OF_HOUR));
// 10. check ChronoField.MINUTE_OF_DAY field supported ?
System.out.println("\n10. LocalDate.isSupported(ChronoField.MINUTE_OF_DAY) ? " +
localDate.isSupported(ChronoField.MINUTE_OF_DAY));
// 11. check ChronoField.HOUR_OF_AMPM field supported ?
System.out.println("\n11. LocalDate.isSupported(ChronoField.HOUR_OF_AMPM) ? " +
localDate.isSupported(ChronoField.HOUR_OF_AMPM));
// 12. check ChronoField.CLOCK_HOUR_OF_AMPM field supported ?
System.out.println("\n12. LocalDate.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM) ? " +
localDate.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM));
// 13. check ChronoField.HOUR_OF_DAY field supported ?
System.out.println("\n13. LocalDate.isSupported(ChronoField.HOUR_OF_DAY) ? " +
localDate.isSupported(ChronoField.HOUR_OF_DAY));
// 14. check ChronoField.CLOCK_HOUR_OF_DAY field supported ?
System.out.println("\n14. LocalDate.isSupported(ChronoField.CLOCK_HOUR_OF_DAY) ? " +
localDate.isSupported(ChronoField.CLOCK_HOUR_OF_DAY));
// 15. check ChronoField.AMPM_OF_DAY field supported ?
System.out.println("\n15. LocalDate.isSupported(ChronoField.AMPM_OF_DAY) ? " +
localDate.isSupported(ChronoField.AMPM_OF_DAY));
// 16. check ChronoField.INSTANT_SECONDS field supported ?
System.out.println("\n16. LocalDate.isSupported(ChronoField.INSTANT_SECONDS) ? " +
localDate.isSupported(ChronoField.INSTANT_SECONDS));
// 17. check ChronoField.OFFSET_SECONDS field supported ?
System.out.print("\n17. LocalDate.isSupported(ChronoField.OFFSET_SECONDS) ? " +
localDate.isSupported(ChronoField.OFFSET_SECONDS));
}
}
Output:
Current system date is =
2022-08-07
1. LocalDate.isSupported(ChronoField.NANO_OF_SECOND) ? false
2. LocalDate.isSupported(ChronoField.NANO_OF_DAY) ? false
3. LocalDate.isSupported(ChronoField.MICRO_OF_SECOND) ? false
4. LocalDate.isSupported(ChronoField.MICRO_OF_DAY) ? false
5. LocalDate.isSupported(ChronoField.MILLI_OF_SECOND) ? false
6. LocalDate.isSupported(ChronoField.MILLI_OF_DAY) ? false
7. LocalDate.isSupported(ChronoField.SECOND_OF_MINUTE) ? false
8. LocalDate.isSupported(ChronoField.SECOND_OF_DAY) ? false
9. LocalDate.isSupported(ChronoField.MINUTE_OF_HOUR) ? false
10. LocalDate.isSupported(ChronoField.MINUTE_OF_DAY) ? false
11. LocalDate.isSupported(ChronoField.HOUR_OF_AMPM) ? false
12. LocalDate.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM) ? false
13. LocalDate.isSupported(ChronoField.HOUR_OF_DAY) ? false
14. LocalDate.isSupported(ChronoField.CLOCK_HOUR_OF_DAY) ? false
15. LocalDate.isSupported(ChronoField.AMPM_OF_DAY) ? false
16. LocalDate.isSupported(ChronoField.INSTANT_SECONDS) ? false
17. LocalDate.isSupported(ChronoField.OFFSET_SECONDS) ? false
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- Java 8 – What are all the Temporal Fields supported by LocalDate ?
- Java 8 – What are all the Temporal Units supported by LocalDate ?
- More Java 8 Date/time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoField.html
- https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalField.html
- https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html
- https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalUnit.html
Happy Coding !!
Happy Learning !!