Duration get(TemporalUnit) method in Java with Examples Last Updated : 26 Nov, 2018 Comments Improve Suggest changes Like Article Like Report The get(TemporalUnit) method of Duration Class in java.time package is used to get the value of the unit passed as the parameter. Only units SECONDS and NANOS ate supported by this method and the rest result in exception. Syntax: public long get(TemporalUnit unit) Parameters: This method accepts a parameter unit which is the TemporalUnit for which the value is to be fetched. Return Value: This method returns a long value of the unit passed as the parameter. Exception: This method throws: DateTimeException: if the unit is not supported. UnsupportedTemporalTypeException: if the unit is not supported. Below examples illustrate the Duration.get() method: Example 1: Java // Java code to illustrate get() method import java.time.Duration; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // Get the text String time = "P2DT3H4M"; // Duration using parse() method Duration duration = Duration.parse(time); // Duration using get() method long getSeconds = duration.get(ChronoUnit.SECONDS); System.out.println("Seconds: " + getSeconds); // Duration using get() method long getNanos = duration.get(ChronoUnit.NANOS); System.out.println("Nanos: " + getNanos); } } Output: Seconds: 183840 Nanos: 0 Example 2: Java // Java code to illustrate get() method import java.time.Duration; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // Get the text String time = "P2DT3H4M"; // Duration using parse() method Duration duration = Duration.parse(time); try { // Duration using get() method long getMinutes = duration.get(ChronoUnit.MINUTES); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output: Exception: java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Minutes Reference: Oracle Doc Comment More infoAdvertise with us Next Article Duration from(TemporalUnit) method in Java with Examples C code_r Follow Improve Article Tags : Java Java-Functions Java-time package Java-Duration Practice Tags : Java Similar Reads Duration from(TemporalUnit) method in Java with Examples The from(TemporalUnit) method of Duration Class in java.time package is used to get a duration from the amount passed as the first parameter in the TemporalUnit. The TemporalUnit can be DAYS, HOURS, etc. Syntax: public static Duration from(TemporalUnit amount) Parameters: This method accepts a param 1 min read Duration from(TemporalUnit) method in Java with Examples The from(TemporalUnit) method of Duration Class in java.time package is used to get a duration from the amount passed as the first parameter in the TemporalUnit. The TemporalUnit can be DAYS, HOURS, etc. Syntax: public static Duration from(TemporalUnit amount) Parameters: This method accepts a param 1 min read Duration of(long, TemporalUnit) method in Java with Examples The of(long, TemporalUnit) method of Duration Class in java.time package is used to get a duration of the amount passed as the first parameter in the TemporalUnit passed as the second parameter. The TemporalUnit can be DAYS, HOURS, etc. Syntax: public static Duration of(long amount, TemporalUnit uni 2 min read Duration plus(long, TemporalUnit) method in Java with Examples The plus(long, TemporalUnit) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified duration added, passed as the parameter. The duration to be added is decided by converting the amountToAdd in the unit passed as the parameters. Syntax: pub 2 min read Duration plus(long, TemporalUnit) method in Java with Examples The plus(long, TemporalUnit) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified duration added, passed as the parameter. The duration to be added is decided by converting the amountToAdd in the unit passed as the parameters. Syntax: pub 2 min read Duration minus(long, TemporalUnit) method in Java with Examples The minus(long, TemporalUnit) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified duration subtracted, passed as the parameter. The duration to be subtracted is decided by converting the amountToAdd in the unit passed as the parameters. 2 min read Like