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

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
    1. Return true, if supported
    2. Returns false, if not supported
  • There are various methods available in LocalDate which can be used after checking if they are supported by LocalTime
    1. get(TemporalField) – gets the value of the specified field from invoking LocalDate as an int
    2. getLong(TemporalField) – gets the value of the specified field from invoking LocalDate 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 LocalDate with the specified field set to a new value
  • Below are the list of Temporal Fields (ChronoField) supported by invoking LocalDate
    1. DAY_OF_WEEK
    2. ALIGNED_DAY_OF_WEEK_IN_MONTH
    3. ALIGNED_DAY_OF_WEEK_IN_YEAR
    4. DAY_OF_MONTH
    5. DAY_OF_YEAR
    6. EPOCH_DAY
    7. ALIGNED_WEEK_OF_MONTH
    8. ALIGNED_WEEK_OF_YEAR
    9. MONTH_OF_YEAR
    10. PROLEPTIC_MONTH
    11. YEAR_OF_ERA
    12. YEAR
    13. ERA
  • Below are the list of Temporal Fields (ChronoField) NOT supported by invoking LocalDate
    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. INSTANT_SECONDS
    17. 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 :

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

References:

Happy Coding !!
Happy Learning !!

Java 8 – What are all the Temporal Units supported by LocalDate ?
Java 8 – What are all the Temporal Units supported by LocalTime ?