Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
dividedby
duration

What is Duration.dividedBy() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

dividedBy() is an instance method of the Duration class which is used to divide the Duration object by a scalar value that is passed as a parameter to the method.

The dividedBy method is defined in the Duration class. The Duration class is defined in the java.time package. To import the Duration class, we check the following import statement:

import java.time.Duration;

Syntax


public Duration dividedBy(long divisor)

Parameters

  • long divisor: This is the value that the Duration object is divided by. It can be either a positive or a negative value.

Return value

This method returns a Duration object that was divided by the passed scalar value.

Code

import java.time.Duration;

public class Main {

    public static void main(String[] args) {
        Duration duration = Duration.ofSeconds(143234);

        int scalarValueToDivide = 2;

        Duration newDuration = duration.dividedBy(scalarValueToDivide);

        System.out.printf("%s / %s = %s", duration.toString()
            .substring(2)
            .replaceAll("(\\d[HMS])(?!$)", "$1 ")
            .toLowerCase(), scalarValueToDivide, newDuration.toString()
            .substring(2)
            .replaceAll("(\\d[HMS])(?!$)", "$1 ")
            .toLowerCase());
    }
}
Implementation of the "dividedBy" method

Code explanation

  • Line 1: We import the Duration class.
  • Line 6: We define a Duration object, using the ofSeconds() method.
  • Line 8: We define the scalar value to divide the Duration object by.
  • Line 10: We divide the Duration object with the scalar value defined in line 8.
  • Line 12: We print the original duration object, the scalar value, and the resultant duration object.

RELATED TAGS

java
communitycreator
dividedby
duration

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring