In this article, we will discuss what are all the Temporal Fields supported by LocalDateTime using isSupported() method provided in Java 1.8 version
1. LocalDateTime & 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 LocalDateTime
- Return true, if supported
- Returns false, if not supported
- There are various methods available in LocalDateTime which can be used after checking if they are supported by LocalDateTime
- get(TemporalField) – gets the value of the specified field from invoking LocalDateTime as an int
- getLong(TemporalField) – gets the value of the specified field from invoking LocalDateTime as a long
- range(TemporalField) – gets the range of valid values for the specified field
- with(TemporalField, long) – returns a copy of invoking LocalDateTime with the specified field set to a new value
- Below are the list of Temporal Fields (ChronoField) supported by invoking LocalDateTime
- 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
- 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 LocalDateTime
- INSTANT_SECONDS
- OFFSET_SECONDS
- There are 30 enum constants in ChronoField out of which 28 are supported by LocalDateTime
- Lets see 2 examples covering each of the above ChronoField ENUM constants
2. Examples on LocalDateTime & TemporalField :
- Temporal Fields supported by LocalDateTime
- Temporal Fields not supported by LocalDateTime
2.1 Temporal Fields supported by LocalDateTime :
- This example covers all the Temporal Fields supported by LocalDateTime
CheckLocalDateTimeIsSupportedUsingTemporalField.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class CheckLocalDateTimeIsSupportedUsingTemporalField {
public static void main(String[] args) {
// get current system date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current system date/time is = " + localDateTime);
// •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
// •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.NANO_OF_SECOND field supported ?
System.out.println("\n1. LocalDateTime.isSupported(ChronoField.NANO_OF_SECOND) ? " +
localDateTime.isSupported(ChronoField.NANO_OF_SECOND));
// 2. check ChronoField.NANO_OF_DAY field supported ?
System.out.println("\n2. LocalDateTime.isSupported(ChronoField.NANO_OF_DAY) ? " +
localDateTime.isSupported(ChronoField.NANO_OF_DAY));
// 3. check ChronoField.MICRO_OF_SECOND field supported ?
System.out.println("\n3. LocalDateTime.isSupported(ChronoField.MICRO_OF_SECOND) ? " +
localDateTime.isSupported(ChronoField.MICRO_OF_SECOND));
// 4. check ChronoField.MICRO_OF_DAY field supported ?
System.out.println("\n4. LocalDateTime.isSupported(ChronoField.MICRO_OF_DAY) ? " +
localDateTime.isSupported(ChronoField.MICRO_OF_DAY));
// 5. check ChronoField.MILLI_OF_SECOND field supported ?
System.out.println("\n5. LocalDateTime.isSupported(ChronoField.MILLI_OF_SECOND) ? " +
localDateTime.isSupported(ChronoField.MILLI_OF_SECOND));
// 6. check ChronoField.MILLI_OF_DAY field supported ?
System.out.println("\n6. LocalDateTime.isSupported(ChronoField.MILLI_OF_DAY) ? " +
localDateTime.isSupported(ChronoField.MILLI_OF_DAY));
// 7. check ChronoField.SECOND_OF_MINUTE field supported ?
System.out.println("\n7. LocalDateTime.isSupported(ChronoField.SECOND_OF_MINUTE) ? " +
localDateTime.isSupported(ChronoField.SECOND_OF_MINUTE));
// 8. check ChronoField.SECOND_OF_DAY field supported ?
System.out.println("\n8. LocalDateTime.isSupported(ChronoField.SECOND_OF_DAY) ? " +
localDateTime.isSupported(ChronoField.SECOND_OF_DAY));
// 9. check ChronoField.MINUTE_OF_HOUR field supported ?
System.out.println("\n9. LocalDateTime.isSupported(ChronoField.MINUTE_OF_HOUR) ? " +
localDateTime.isSupported(ChronoField.MINUTE_OF_HOUR));
// 10. check ChronoField.MINUTE_OF_DAY field supported ?
System.out.println("\n10. LocalDateTime.isSupported(ChronoField.MINUTE_OF_DAY) ? " +
localDateTime.isSupported(ChronoField.MINUTE_OF_DAY));
// 11. check ChronoField.HOUR_OF_AMPM field supported ?
System.out.println("\n11. LocalDateTime.isSupported(ChronoField.HOUR_OF_AMPM) ? " +
localDateTime.isSupported(ChronoField.HOUR_OF_AMPM));
// 12. check ChronoField.CLOCK_HOUR_OF_AMPM field supported ?
System.out.println("\n12. LocalDateTime.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM) ? " +
localDateTime.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM));
// 13. check ChronoField.HOUR_OF_DAY field supported ?
System.out.println("\n13. LocalDateTime.isSupported(ChronoField.HOUR_OF_DAY) ? " +
localDateTime.isSupported(ChronoField.HOUR_OF_DAY));
// 14. check ChronoField.CLOCK_HOUR_OF_DAY field supported ?
System.out.println("\n14. LocalDateTime.isSupported(ChronoField.CLOCK_HOUR_OF_DAY) ? " +
localDateTime.isSupported(ChronoField.CLOCK_HOUR_OF_DAY));
// 15. check ChronoField.AMPM_OF_DAY field supported ?
System.out.println("\n15. LocalDateTime.isSupported(ChronoField.AMPM_OF_DAY) ? " +
localDateTime.isSupported(ChronoField.AMPM_OF_DAY));
// 16. check ChronoField.DAY_OF_WEEK field supported ?
System.out.println("\n16. LocalDateTime.isSupported(ChronoField.DAY_OF_WEEK) ? " +
localDateTime.isSupported(ChronoField.DAY_OF_WEEK));
// 17. check ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH field supported ?
System.out.println("\n17. LocalDateTime"
+ ".isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH) ? " +
localDateTime.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH));
// 18. check ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR field supported ?
System.out.println("\n18. LocalDateTime"
+ ".isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR) ? " +
localDateTime.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR));
// 19. check ChronoField.DAY_OF_MONTH field supported ?
System.out.println("\n19. LocalDateTime.isSupported(ChronoField.DAY_OF_MONTH) ? " +
localDateTime.isSupported(ChronoField.DAY_OF_MONTH));
// 20. check ChronoField.DAY_OF_YEAR field supported ?
System.out.println("\n20. LocalDateTime.isSupported(ChronoField.DAY_OF_YEAR) ? " +
localDateTime.isSupported(ChronoField.DAY_OF_YEAR));
// 21. check ChronoField.EPOCH_DAY field supported ?
System.out.println("\n21. LocalDateTime.isSupported(ChronoField.EPOCH_DAY) ? " +
localDateTime.isSupported(ChronoField.EPOCH_DAY));
// 22. check ChronoField.ALIGNED_WEEK_OF_MONTH field supported ?
System.out.println("\n22. LocalDateTime.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH) ? " +
localDateTime.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH));
// 23. check ChronoField.ALIGNED_WEEK_OF_YEAR field supported ?
System.out.println("\n23. LocalDateTime.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR) ? " +
localDateTime.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR));
// 24. check ChronoField.MONTH_OF_YEAR field supported ?
System.out.println("\n24. LocalDateTime.isSupported(ChronoField.MONTH_OF_YEAR) ? " +
localDateTime.isSupported(ChronoField.MONTH_OF_YEAR));
// 25. check ChronoField.PROLEPTIC_MONTH field supported ?
System.out.println("\n25. LocalDateTime.isSupported(ChronoField.PROLEPTIC_MONTH) ? " +
localDateTime.isSupported(ChronoField.PROLEPTIC_MONTH));
// 26. check ChronoField.YEAR_OF_ERA field supported ?
System.out.println("\n26. LocalDateTime.isSupported(ChronoField.YEAR_OF_ERA) ? " +
localDateTime.isSupported(ChronoField.YEAR_OF_ERA));
// 27. check ChronoField.YEAR field supported ?
System.out.println("\n27. LocalDateTime.isSupported(ChronoField.YEAR) ? " +
localDateTime.isSupported(ChronoField.YEAR));
// 28. check ChronoField.ERA field supported ?
System.out.print("\n28. LocalDateTime.isSupported(ChronoField.ERA) ? " +
localDateTime.isSupported(ChronoField.ERA));
}
}
Output:
Current system date/time is = 2022-08-10T20:39:53.211582400
1. LocalDateTime.isSupported(ChronoField.NANO_OF_SECOND) ? true
2. LocalDateTime.isSupported(ChronoField.NANO_OF_DAY) ? true
3. LocalDateTime.isSupported(ChronoField.MICRO_OF_SECOND) ? true
4. LocalDateTime.isSupported(ChronoField.MICRO_OF_DAY) ? true
5. LocalDateTime.isSupported(ChronoField.MILLI_OF_SECOND) ? true
6. LocalDateTime.isSupported(ChronoField.MILLI_OF_DAY) ? true
7. LocalDateTime.isSupported(ChronoField.SECOND_OF_MINUTE) ? true
8. LocalDateTime.isSupported(ChronoField.SECOND_OF_DAY) ? true
9. LocalDateTime.isSupported(ChronoField.MINUTE_OF_HOUR) ? true
10. LocalDateTime.isSupported(ChronoField.MINUTE_OF_DAY) ? true
11. LocalDateTime.isSupported(ChronoField.HOUR_OF_AMPM) ? true
12. LocalDateTime.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM) ? true
13. LocalDateTime.isSupported(ChronoField.HOUR_OF_DAY) ? true
14. LocalDateTime.isSupported(ChronoField.CLOCK_HOUR_OF_DAY) ? true
15. LocalDateTime.isSupported(ChronoField.AMPM_OF_DAY) ? true
16. LocalDateTime.isSupported(ChronoField.DAY_OF_WEEK) ? true
17. LocalDateTime.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH) ? true
18. LocalDateTime.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR) ? true
19. LocalDateTime.isSupported(ChronoField.DAY_OF_MONTH) ? true
20. LocalDateTime.isSupported(ChronoField.DAY_OF_YEAR) ? true
21. LocalDateTime.isSupported(ChronoField.EPOCH_DAY) ? true
22. LocalDateTime.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH) ? true
23. LocalDateTime.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR) ? true
24. LocalDateTime.isSupported(ChronoField.MONTH_OF_YEAR) ? true
25. LocalDateTime.isSupported(ChronoField.PROLEPTIC_MONTH) ? true
26. LocalDateTime.isSupported(ChronoField.YEAR_OF_ERA) ? true
27. LocalDateTime.isSupported(ChronoField.YEAR) ? true
28. LocalDateTime.isSupported(ChronoField.ERA) ? true
2.2 Temporal Fields not supported by LocalDateTime :
- This example covers Temporal Fields not supported by LocalDateTime
CheckLocalDateTimeIsSupportedUsingTemporalField2.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class CheckLocalDateTimeIsSupportedUsingTemporalField2 {
public static void main(String[] args) {
// get current system date
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current system date/time is = " + localDateTime);
// •INSTANT_SECONDS
// •OFFSET_SECONDS
// Above ChronoField instances will return false
// 1. check ChronoField.INSTANT_SECONDS field supported ?
System.out.println("\n1. LocalDateTime.isSupported(ChronoField.INSTANT_SECONDS) ? " +
localDateTime.isSupported(ChronoField.INSTANT_SECONDS));
// 2. check ChronoField.OFFSET_SECONDS field supported ?
System.out.print("\n2. LocalDateTime.isSupported(ChronoField.OFFSET_SECONDS) ? " +
localDateTime.isSupported(ChronoField.OFFSET_SECONDS));
}
}
Output:
Current system date/time is = 2022-08-11T19:16:24.435566100
1. LocalDateTime.isSupported(ChronoField.INSTANT_SECONDS) ? false
2. LocalDateTime.isSupported(ChronoField.OFFSET_SECONDS) ? false
Related Articles:
- Java 8 – LocalDateTime with method details and examples
- Java 8 – How to get Date and Time fields from LocalDateTime ?
- Java 8 – How to form LocalDateTime passing Date and Time fields ?
- Java 8 – How to form LocalDateTime passing LocalDate and LocalTime ?
- Java 8 – How to form LocalDateTime passing Instant and ZoneId ?
- Java 8 – How to form LocalDateTime passing Second/Nano and ZoneOffset ?
- Java 8 – How to parse LocalDateTime in String form ?
- Java 8 – How to convert String to LocalDateTime ?
- Java 8 – How to convert LocalDateTime to String ?
- Java 8 – How to convert LocalDateTime in different formats ?
- Java 8 – How to convert LocalDateTime in different Format Style ?
- Java 8 – How to convert LocalDateTime to ZonedDateTime ?
- Java 8 – How to convert LocalDateTime to an OffsetDateTime ?
- Java 8 – How to convert LocalDateTime to an Instant ?
- Java 8 – How to extract LocalDateTime and LocalTime from LocalDateTime ?
- Java 8 – How to convert LocalDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDateTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime in different ways ?
- Java 8 – How to add Date and Time fields to LocalDateTime ?
- Java 8 – How to subtract Date and Time fields from LocalDateTime ?
- Java 8 – How to alter Date and Time fields of LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is Before another LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is After another LocalDateTime ?
- Java 8 – How to compare two LocalDateTime instances ?
- Java 8 – How to find difference between two LocalDateTime using Period & Duration ?
- Java 8 – What are all the Temporal Fields supported by LocalDateTime ?
- Java 8 – What are all the Temporal Units supported by LocalDateTime ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.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 !!