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

In this article, we will discuss what are all the Temporal Units supported by LocalDate using isSupported() method provided in Java 1.8 version

1. LocalDate & TemporalUnit :

  • A unit of date-time, such as Days or Hours
  • Measurement of time is built on units, such as years, months, days, hours, minutes and seconds
  • Implementations of this interface represent those units
  • An instance of this interface represents the unit itself, rather than an amount of the unit
  • The most commonly used units are defined in ChronoUnit
  • isSupported(TemporalUnit) – checks if the specified Unit 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 LocalDate
    1. minus(long, TemporalUnit) – Returns a copy of this date with the specified amount subtracted
    2. plus(long amountToAdd, TemporalUnit) – Returns a copy of this date with the specified amount added
    3. until(Temporal, TemporalUnit) – Calculates the amount of time until another date in terms of the specified unit
  • Below are the list of Temporal Units (ChronoUnit) supported by invoking LocalDate
    1. DAYS
    2. WEEKS
    3. MONTHS
    4. YEARS
    5. DECADES
    6. CENTURIES
    7. MILLENNIA
    8. ERAS
  • Below are the list of Temporal Units (ChronoUnit) not supported by invoking LocalDate
    1. NANOS
    2. MICROS
    3. MILLIS
    4. SECONDS
    5. MINUTES
    6. HOURS
    7. HALF_DAYS
    8. FOREVER
  • There are 16 enum constants in ChronoUnit out of which 8 are supported by LocalDate
  • Lets see 2 examples covering each of the above ChronoUnit ENUM constants

2. Examples on LocalDate & TemporalUnit :

  1. Temporal Units supported by LocalDate
  2. Temporal Units not supported by LocalDate

2.1 Temporal Units supported by LocalDate :

  • This example covers all the Temporal Units supported by LocalDate

CheckLocalDateIsSupportedUsingTemporalUnit.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class CheckLocalDateIsSupportedUsingTemporalUnit {

	public static void main(String[] args) {

		// get current system date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current system date is = \n" + localDate);


		//		•DAYS 
		//		•WEEKS 
		//		•MONTHS 
		//		•YEARS 
		//		•DECADES 
		//		•CENTURIES 
		//		•MILLENNIA 
		//		•ERAS 
		//		Except above, all other ChronoUnit instances will return false


		// 1. check ChronoUnit.DAYS field supported ?
		System.out.println("\n1. LocalDate.isSupported(ChronoUnit.DAYS) ? " + 
				localDate.isSupported(ChronoUnit.DAYS));


		// 2. check ChronoUnit.WEEKS field supported ?
		System.out.println("\n2. LocalDate.isSupported(ChronoUnit.WEEKS) ? " + 
				localDate.isSupported(ChronoUnit.WEEKS));


		// 3. check ChronoUnit.MONTHS field supported ?
		System.out.println("\n3. LocalDate.isSupported(ChronoUnit.MONTHS) ? " + 
				localDate.isSupported(ChronoUnit.MONTHS));


		// 4. check ChronoUnit.YEARS field supported ?
		System.out.println("\n4. LocalDate.isSupported(ChronoUnit.YEARS) ? " + 
				localDate.isSupported(ChronoUnit.YEARS));


		// 5. check ChronoUnit.DECADES field supported ?
		System.out.println("\n5. LocalDate.isSupported(ChronoUnit.DECADES) ? " + 
				localDate.isSupported(ChronoUnit.DECADES));


		// 6. check ChronoUnit.CENTURIES field supported ?
		System.out.println("\n6. LocalDate.isSupported(ChronoUnit.CENTURIES) ? " + 
				localDate.isSupported(ChronoUnit.CENTURIES));


		// 7. check ChronoUnit.MILLENNIA field supported ?
		System.out.println("\n7. LocalDate.isSupported(ChronoUnit.MILLENNIA) ? " + 
				localDate.isSupported(ChronoUnit.MILLENNIA));


		// 8. check ChronoUnit.ERAS field supported ?
		System.out.print("\n8. LocalDate.isSupported(ChronoUnit.ERAS) ? " + 
				localDate.isSupported(ChronoUnit.ERAS));
	}
}

Output:

Current system date is = 
2022-08-07

1. LocalDate.isSupported(ChronoUnit.DAYS) ? true

2. LocalDate.isSupported(ChronoUnit.WEEKS) ? true

3. LocalDate.isSupported(ChronoUnit.MONTHS) ? true

4. LocalDate.isSupported(ChronoUnit.YEARS) ? true

5. LocalDate.isSupported(ChronoUnit.DECADES) ? true

6. LocalDate.isSupported(ChronoUnit.CENTURIES) ? true

7. LocalDate.isSupported(ChronoUnit.MILLENNIA) ? true

8. LocalDate.isSupported(ChronoUnit.ERAS) ? true

2.2 Temporal Units not supported by LocalDate :

  • This example covers Temporal Units not supported by LocalDate

CheckLocalDateIsSupportedUsingTemporalUnit2.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class CheckLocalDateIsSupportedUsingTemporalUnit2 {

	public static void main(String[] args) {

		// get current system date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current system date is = \n" + localDate);


		//		•NANOS 
		//		•MICROS 
		//		•MILLIS 
		//		•SECONDS 
		//		•MINUTES 
		//		•HOURS 
		//		•HALF_DAYS 
		// 		•FOREVER
		// 		Above ChronoUnit instances will return false


		// 1. check ChronoUnit.NANOS field supported ?
		System.out.println("\n1. LocalDate.isSupported(ChronoUnit.NANOS) ? " + 
				localDate.isSupported(ChronoUnit.NANOS));


		// 2. check ChronoUnit.MICROS field supported ?
		System.out.println("\n2. LocalDate.isSupported(ChronoUnit.MICROS) ? " + 
				localDate.isSupported(ChronoUnit.MICROS));


		// 3. check ChronoUnit.MILLIS field supported ?
		System.out.println("\n3. LocalDate.isSupported(ChronoUnit.MILLIS) ? " + 
				localDate.isSupported(ChronoUnit.MILLIS));


		// 4. check ChronoUnit.SECONDS field supported ?
		System.out.println("\n4. LocalDate.isSupported(ChronoUnit.SECONDS) ? " + 
				localDate.isSupported(ChronoUnit.SECONDS));


		// 5. check ChronoUnit.MINUTES field supported ?
		System.out.println("\n5. LocalDate.isSupported(ChronoUnit.MINUTES) ? " + 
				localDate.isSupported(ChronoUnit.MINUTES));


		// 6. check ChronoUnit.HOURS field supported ?
		System.out.println("\n6. LocalDate.isSupported(ChronoUnit.HOURS) ? " + 
				localDate.isSupported(ChronoUnit.HOURS));


		// 7. check ChronoUnit.HALF_DAYS field supported ?
		System.out.println("\n7. LocalDate.isSupported(ChronoUnit.HALF_DAYS) ? " + 
				localDate.isSupported(ChronoUnit.HALF_DAYS));


		// 8. check ChronoUnit.FOREVER field supported ?
		System.out.print("\n8. LocalDate.isSupported(ChronoUnit.FOREVER) ? " + 
				localDate.isSupported(ChronoUnit.FOREVER));
	}
}

Output:

Current system date is = 
2022-08-07

1. LocalDate.isSupported(ChronoUnit.NANOS) ? false

2. LocalDate.isSupported(ChronoUnit.MICROS) ? false

3. LocalDate.isSupported(ChronoUnit.MILLIS) ? false

4. LocalDate.isSupported(ChronoUnit.SECONDS) ? false

5. LocalDate.isSupported(ChronoUnit.MINUTES) ? false

6. LocalDate.isSupported(ChronoUnit.HOURS) ? false

7. LocalDate.isSupported(ChronoUnit.HALF_DAYS) ? false

8. LocalDate.isSupported(ChronoUnit.FOREVER) ? false

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to get Date and Time fields from LocalDateTime ?
Java 8 – What are all the Temporal Fields supported by LocalDate ?