Java 8 – What are all the Temporal Fields supported by LocalDateTime ?

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
    1. Return true, if supported
    2. Returns false, if not supported
  • There are various methods available in LocalDateTime which can be used after checking if they are supported by LocalDateTime
    1. get(TemporalField) – gets the value of the specified field from invoking LocalDateTime as an int
    2. getLong(TemporalField) – gets the value of the specified field from invoking LocalDateTime as a long
    3. range(TemporalField) – gets the range of valid values for the specified field
    4. 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
    1. NANO_OF_SECOND
    2. NANO_OF_DAY
    3. MICRO_OF_SECOND
    4. MICRO_OF_DAY
    5. MILLI_OF_SECOND
    6. MILLI_OF_DAY
    7. SECOND_OF_MINUTE
    8. SECOND_OF_DAY
    9. MINUTE_OF_HOUR
    10. MINUTE_OF_DAY
    11. HOUR_OF_AMPM
    12. CLOCK_HOUR_OF_AMPM
    13. HOUR_OF_DAY
    14. CLOCK_HOUR_OF_DAY
    15. AMPM_OF_DAY
    16. DAY_OF_WEEK
    17. ALIGNED_DAY_OF_WEEK_IN_MONTH
    18. ALIGNED_DAY_OF_WEEK_IN_YEAR
    19. DAY_OF_MONTH
    20. DAY_OF_YEAR
    21. EPOCH_DAY
    22. ALIGNED_WEEK_OF_MONTH
    23. ALIGNED_WEEK_OF_YEAR
    24. MONTH_OF_YEAR
    25. PROLEPTIC_MONTH
    26. YEAR_OF_ERA
    27. YEAR
    28. ERA
  • Below are the list of Temporal Fields (ChronoField) NOT supported by invoking LocalDateTime
    1. INSTANT_SECONDS
    2. 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 :

  1. Temporal Fields supported by LocalDateTime
  2. 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – What are all the Temporal Units supported by LocalDateTime ?
Java 8 – How to find difference between two LocalDateTime using Period/Duration ?