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