Close

Java Date Time - LocalTime.isSupported() Examples

Java Date Time Java Java API 


Class:

java.time.LocalTime

java.lang.Objectjava.lang.Objectjava.time.LocalTimejava.time.LocalTimejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Methods:

public boolean isSupported(TemporalField field)

This method checks if the provided TemporalField supported by this instance.

If this method returns false, then calling the range, get and with methods with corresponding value will throw an exception.


public boolean isSupported(TemporalUnit unit)

This methods check if the provided TemporalUnit is supported by this instance.


Examples


package com.logicbig.example.localtime;

import java.time.LocalTime;
import java.time.temporal.ChronoField;

public class IsSupportedExample {

public static void main (String... args) {
LocalTime d = LocalTime.now();
boolean b = d.isSupported(ChronoField.NANO_OF_DAY);
System.out.println(b);

boolean b2 = d.isSupported(ChronoField.MINUTE_OF_HOUR);
System.out.println(b2);

boolean b3 = d.isSupported(ChronoField.OFFSET_SECONDS);
System.out.println(b3);

boolean b4 = d.isSupported(ChronoField.DAY_OF_WEEK);
System.out.println(b4);

}
}

Output

true
true
false
false




package com.logicbig.example.localtime;

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

public class IsSupportedExample2 {

public static void main (String... args) {
LocalTime d = LocalTime.now();

boolean b = d.isSupported(ChronoUnit.HOURS);
System.out.println(b);

boolean b2 = d.isSupported(ChronoUnit.DAYS);
System.out.println(b2);

boolean b3 = d.isSupported(ChronoUnit.MONTHS);
System.out.println(b3);

boolean b4 = d.isSupported(ChronoUnit.MICROS);
System.out.println(b4);

}

}

Output

true
false
false
true




package com.logicbig.example.localtime;

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

public class IsSupportedExample3 {

public static void main (String... args) {
LocalTime d = LocalTime.now();

boolean b = d.isSupported(ChronoUnit.MONTHS);
System.out.println(b);

LocalTime d2 = d.plus(5, ChronoUnit.MONTHS);
System.out.println(d2);

}

}

Output

Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Months
at java.time.LocalTime.plus(LocalTime.java:1055)
at com.logicbig.example.localtime.IsSupportedExample3.main(IsSupportedExample3.java:19)
... 6 more




See Also