ADVERTISEMENT
ADVERTISEMENT

Java Duration Class | plusHours() Method with Example

Duration Class plusHours() method: Here, we are going to learn about the plusHours() method of Duration Class with its syntax and example.
Submitted by Preeti Jain, on May 16, 2020

Duration Class plusHours() method

  • plusHours() method is available in java.time package.
  • plusHours() method is used to add the given duration in hours to this Duration and return the Duration.
  • plusHours() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • plusHours() method may throw an exception at the time of performing addition.
    ArithmeticException: This exception may throw when the calculated results exceed the length of this Duration.

Syntax:

    public Duration plusHours(long hrs_val);

Parameter(s):

  • long hrs_val – represents the hours value to add to this Duration.

Return value:

The return type of this method is Duration, it returns the Duration that holds the value added the given hours to this Duration.

Example:

// Java program to demonstrate the example 
// of plusHours(long hrs_val) method of Duration

import java.time.*;

public class PlusHoursOfDuration {
    public static void main(String args[]) {
        long hours = 5;

        // Instantiates two Duration objects
        Duration du1 = Duration.ofHours(6);
        Duration du2 = Duration.parse("P2DT2H20M");

        // Display du1, du2 
        System.out.println("du1: " + du1);
        System.out.println("du2: " + du2);
        System.out.println("hours to add: " + 5);

        System.out.println();

        // adds the given duration in hours with this 
        // Duration du1 and returns the Duration i.e. 
        // here we are adding the given 5 hours with 
        // this du1 that holds the value of 6 hours 
        // (6H +5H = 11H)
        Duration plus_val = du1.plusHours(hours);

        // Display plus_val
        System.out.println("du1.plusHours(hours): " + plus_val);

        // adds the given duration in hours with this 
        // Duration du2 and returns the Duration 
        // i.e. here we are adding the given 5 hours 
        // with this du2 that holds the value of 
        // 2D:2H:20M (2D = 48H , 2H +5H =55H)
        plus_val = du2.plusHours(hours);

        // Display plus_val
        System.out.println("du2.plusHours(hours): " + plus_val);
    }
}

Output

du1: PT6H
du2: PT50H20M
hours to add: 5

du1.plusHours(hours): PT11H
du2.plusHours(hours): PT55H20M
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.