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

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

1. LocalTime & 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 LocalTime
    1. Return true, if supported
    2. Returns false, if not supported
  • There are various methods available in LocalTime which can be used after checking if they are supported by LocalTime
    1. minus(long, TemporalUnit) – Returns a copy of invoking LocalTime with the specified amount subtracted
    2. plus(long, TemporalUnit) – Returns a copy of invoking LocalTime with the specified amount added
    3. truncatedTo(TemporalUnit) – Returns a copy of invoking LocalTime with the time truncated
    4. until(Temporal, TemporalUnit) – Calculates the amount of time until another time in terms of the specified unit
  • Below are the list of Temporal Units (ChronoUnit) supported by invoking LocalTime
    1. NANOS
    2. MICROS
    3. MILLIS
    4. SECONDS
    5. MINUTES
    6. HOURS
    7. HALF_DAYS
  • Below are the list of Temporal Units (ChronoUnit) NOT supported by invoking LocalTime
    1. DAYS
    2. WEEKS
    3. MONTHS
    4. YEARS
    5. DECADES
    6. CENTURIES
    7. MILLENNIA
    8. ERAS
    9. FOREVER
  • There are 16 enum constants in ChronoUnit out of which 7 are supported by LocalTime
  • Lets see 2 examples covering each of the above ChronoUnit ENUM constants

2. Examples on LocalTime & TemporalUnit :

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

2.1 Temporal Units supported by LocalTime :

  • This example covers all the Temporal Units supported by LocalTime

CheckLocalTimeIsSupportedUsingTemporalUnit.java

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

import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class CheckLocalTimeIsSupportedUsingTemporalUnit {

	public static void main(String[] args) {

		// get current system time
		LocalTime localTime = LocalTime.now();
		System.out.println("Current system time is = \n" + localTime);


		//		•NANOS 
		//		•MICROS 
		//		•MILLIS 
		//		•SECONDS 
		//		•MINUTES 
		//		•HOURS 
		//		•HALF_DAYS 
		//		Except above, all other ChronoUnit instances will return false


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


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


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


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


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


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


		// 7. check ChronoUnit.HALF_DAYS field supported ?
		System.out.print("\n7. LocalTime.isSupported(ChronoUnit.HALF_DAYS) ? " + 
				localTime.isSupported(ChronoUnit.HALF_DAYS));
	}
}

Output:

Current system time is = 
18:46:24.051952100

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

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

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

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

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

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

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

2.2 Temporal Units not supported by LocalTime :

  • This example covers Temporal Units not supported by LocalTime

CheckLocalTimeIsSupportedUsingTemporalUnit2.java

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

import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class CheckLocalTimeIsSupportedUsingTemporalUnit2 {

	public static void main(String[] args) {

		// get current system time
		LocalTime localTime = LocalTime.now();
		System.out.println("Current system time is = \n" + localTime);


		//		•DAYS 
		//		•WEEKS 
		//		•MONTHS 
		//		•YEARS 
		//		•DECADES 
		//		•CENTURIES 
		//		•MILLENNIA 
		//		•ERAS 
		// 		•FOREVER


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


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


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


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


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


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


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


		// 8. check ChronoUnit.ERAS field supported ?
		System.out.println("\n8. LocalTime.isSupported(ChronoUnit.ERAS) ? " + 
				localTime.isSupported(ChronoUnit.ERAS));


		// 9. check ChronoUnit.FOREVER field supported ?
		System.out.print("\n9. LocalTime.isSupported(ChronoUnit.FOREVER) ? " + 
				localTime.isSupported(ChronoUnit.FOREVER));
	}
}

Output:

Current system time is = 
09:27:11.466151200

1. LocalTime.isSupported(ChronoUnit.DAYS) ? false

2. LocalTime.isSupported(ChronoUnit.WEEKS) ? false

3. LocalTime.isSupported(ChronoUnit.MONTHS) ? false

4. LocalTime.isSupported(ChronoUnit.YEARS) ? false

5. LocalTime.isSupported(ChronoUnit.DECADES) ? false

6. LocalTime.isSupported(ChronoUnit.CENTURIES) ? false

7. LocalTime.isSupported(ChronoUnit.MILLENNIA) ? false

8. LocalTime.isSupported(ChronoUnit.ERAS) ? false

9. LocalTime.isSupported(ChronoUnit.FOREVER) ? false

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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