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