CodesJava

Easy learning with example program codes

Java 8 Duration class


The java.time.Duration class is used to measures time in seconds and nanoseconds.

Java Duration class methods

Method Description
Temporal addTo(Temporal temporal) It is used to add this duration to the specified temporal object.
static Duration between(Temporal startInclusive, Temporal endExclusive) It is used to obtain a Duration representing the duration between two temporal objects.
long get(TemporalUnit unit) It is used to get the value of the requested unit.
boolean isNegative() It is used to check if this duration is negative, excluding zero.
boolean isZero() It is used to check if this duration is zero length.
Duration minus(Duration duration) It is used to return a copy of this duration with the specified duration subtracted.
Duration plus(Duration duration) It is used to return a copy of this duration with the specified duration added.

Example

package com.codesjava;
 
import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
 
public class TestExample {
	public static void main(String args[]){
	    Duration duration = Duration.between(LocalTime.NOON,LocalTime.MAX); 
	    //get() method
	    System.out.println(duration.get(ChronoUnit.SECONDS));    
 
	    //isNegative() method
	    System.out.println(duration.isNegative()); 
	}  
}

Output

43199
false
Posted in Date   


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki