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

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

1. LocalDateTime & 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 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. minus(long, TemporalUnit) – Returns a copy of invoking LocalDateTime with the specified amount subtracted
    2. plus(long amountToAdd, TemporalUnit) – Returns a copy of invoking LocalDateTime with the specified amount added
    3. until(Temporal, TemporalUnit) – Calculates the amount of time until another LocalDateTime in terms of the specified unit
    4. truncatedTo(TemporalUnit) – Returns a copy of invoking LocalDateTime with the time truncated
  • Below are the list of Temporal Units (ChronoUnit) supported by invoking LocalDateTime
    1. NANOS
    2. MICROS
    3. MILLIS
    4. SECONDS
    5. MINUTES
    6. HOURS
    7. HALF_DAYS
    8. DAYS
    9. WEEKS
    10. MONTHS
    11. YEARS
    12. DECADES
    13. CENTURIES
    14. MILLENNIA
    15. ERAS
  • Below are the list of Temporal Units (ChronoUnit) not supported by invoking LocalDateTime
    1. FOREVER
  • There are 16 enum constants in ChronoUnit out of which 15 are supported by LocalDateTime
  • Lets see 2 examples covering each of the above ChronoUnit ENUM constants

2. Examples on LocalDateTime & TemporalUnit :

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

2.1 Temporal Units supported by LocalDateTime :

  • This example covers all the Temporal Units supported by LocalDateTime

CheckLocalDateTimeIsSupportedUsingTemporalUnit.java

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

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class CheckLocalDateTimeIsSupportedUsingTemporalUnit {

	public static void main(String[] args) {

		// get current system date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Current system date/time is = " + localDateTime);


		//		•NANOS 
		//		•MICROS 
		//		•MILLIS 
		//		•SECONDS 
		//		•MINUTES 
		//		•HOURS 
		//		•HALF_DAYS 
		//		•DAYS 
		//		•WEEKS 
		//		•MONTHS 
		//		•YEARS 
		//		•DECADES 
		//		•CENTURIES 
		//		•MILLENNIA 
		//		•ERAS 
		//		Except above, all other ChronoUnit instances will return false


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


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


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


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


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


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


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


		// 8. check ChronoUnit.DAYS field supported ?
		System.out.println("\n8. LocalDateTime.isSupported(ChronoUnit.DAYS) ? " + 
				localDateTime.isSupported(ChronoUnit.DAYS));


		// 9. check ChronoUnit.WEEKS field supported ?
		System.out.println("\n9. LocalDateTime.isSupported(ChronoUnit.WEEKS) ? " + 
				localDateTime.isSupported(ChronoUnit.WEEKS));


		// 10. check ChronoUnit.MONTHS field supported ?
		System.out.println("\n10. LocalDateTime.isSupported(ChronoUnit.MONTHS) ? " + 
				localDateTime.isSupported(ChronoUnit.MONTHS));


		// 11. check ChronoUnit.YEARS field supported ?
		System.out.println("\n11. LocalDateTime.isSupported(ChronoUnit.YEARS) ? " + 
				localDateTime.isSupported(ChronoUnit.YEARS));


		// 12. check ChronoUnit.DECADES field supported ?
		System.out.println("\n12. LocalDateTime.isSupported(ChronoUnit.DECADES) ? " + 
				localDateTime.isSupported(ChronoUnit.DECADES));


		// 13. check ChronoUnit.CENTURIES field supported ?
		System.out.println("\n13. LocalDateTime.isSupported(ChronoUnit.CENTURIES) ? " + 
				localDateTime.isSupported(ChronoUnit.CENTURIES));


		// 14. check ChronoUnit.MILLENNIA field supported ?
		System.out.println("\n14. LocalDateTime.isSupported(ChronoUnit.MILLENNIA) ? " + 
				localDateTime.isSupported(ChronoUnit.MILLENNIA));


		// 15. check ChronoUnit.ERAS field supported ?
		System.out.print("\n15. LocalDateTime.isSupported(ChronoUnit.ERAS) ? " + 
				localDateTime.isSupported(ChronoUnit.ERAS));
	}
}

Output:

Current system date/time is = 2022-08-11T17:02:07.497086900

1. LocalDateTime.isSupported(ChronoUnit.NANOS) ? true

2. LocalDateTime.isSupported(ChronoUnit.MICROS) ? true

3. LocalDateTime.isSupported(ChronoUnit.MILLIS) ? true

4. LocalDateTime.isSupported(ChronoUnit.SECONDS) ? true

5. LocalDateTime.isSupported(ChronoUnit.MINUTES) ? true

6. LocalDateTime.isSupported(ChronoUnit.HOURS) ? true

7. LocalDateTime.isSupported(ChronoUnit.HALF_DAYS) ? true

8. LocalDateTime.isSupported(ChronoUnit.DAYS) ? true

9. LocalDateTime.isSupported(ChronoUnit.WEEKS) ? true

10. LocalDateTime.isSupported(ChronoUnit.MONTHS) ? true

11. LocalDateTime.isSupported(ChronoUnit.YEARS) ? true

12. LocalDateTime.isSupported(ChronoUnit.DECADES) ? true

13. LocalDateTime.isSupported(ChronoUnit.CENTURIES) ? true

14. LocalDateTime.isSupported(ChronoUnit.MILLENNIA) ? true

15. LocalDateTime.isSupported(ChronoUnit.ERAS) ? true

2.2 Temporal Units not supported by LocalDateTime :

  • This example covers Temporal Units not supported by LocalDateTime

CheckLocalDateIsSupportedUsingTemporalUnit2.java

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

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class CheckLocalDateTimeIsSupportedUsingTemporalUnit2 {

	public static void main(String[] args) {

		// get current system date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Current system date/time is = " + localDateTime);

		// 		•FOREVER
		// 		Above ChronoUnit instances will return false


		// 1. check ChronoUnit.FOREVER field supported ?
		System.out.print("\n1. LocalDateTime.isSupported(ChronoUnit.FOREVER) ? " + 
				localDateTime.isSupported(ChronoUnit.FOREVER));
	}
}

Output:

Current system date/time is = 2022-08-11T17:02:25.484106400

1. LocalDateTime.isSupported(ChronoUnit.FOREVER) ? false

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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