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
- Return true, if supported
- Returns false, if not supported
- There are various methods available in LocalTime which can be used after checking if they are supported by LocalTime
- minus(long, TemporalUnit) – Returns a copy of invoking LocalTime with the specified amount subtracted
- plus(long, TemporalUnit) – Returns a copy of invoking LocalTime with the specified amount added
- truncatedTo(TemporalUnit) – Returns a copy of invoking LocalTime with the time truncated
- 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
- NANOS
- MICROS
- MILLIS
- SECONDS
- MINUTES
- HOURS
- HALF_DAYS
- Below are the list of Temporal Units (ChronoUnit) NOT supported by invoking LocalTime
- DAYS
- WEEKS
- MONTHS
- YEARS
- DECADES
- CENTURIES
- MILLENNIA
- ERAS
- 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 :
- Temporal Units supported by LocalTime
- 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:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html
- https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalUnit.html
- https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoField.html
- https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalField.html
Happy Coding !!
Happy Learning !!