Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Duration isZero() method in Java with Examples
Next article icon

Duration abs() method in Java with Examples

Last Updated : 26 Nov, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

The abs() method of Duration Class in java.time package is used to get an immutable copy of this duration with the absolute duration.

Syntax:

  public Duration abs()  

Parameters: This method do not accepts any parameter.

Return Value: This method returns a Duration which is an immutable copy of the existing duration with the absolute duration.

Exception: This method throws ArithmeticException if numeric overflow occurs.

Below examples illustrate the Duration.abs() method:

Example 1:




// Java code to illustrate abs() method
  
import java.time.Duration;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Duration 1 using parse() method
        Duration duration1
            = Duration.parse("P2DT3H4M");
  
        // Get the duration abs using abs() method
        System.out.println(duration1.abs());
    }
}
 
 
Output:
  PT51H4M  

Example 2:




// Java code to illustrate abs() method
  
import java.time.Duration;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Duration
        Duration duration2
            = Duration.ofDays(-5);
  
        // Get the duration abs using abs() method
        System.out.println(duration2.abs());
    }
}
 
 
Output:
  PT120H  

Reference: Oracle Doc



Next Article
Duration isZero() method in Java with Examples

C

code_r
Improve
Article Tags :
  • Java
  • Java-Duration
  • Java-Functions
  • Java-time package
Practice Tags :
  • Java

Similar Reads

  • Duration toDays() method in Java with Examples
    The toDays() method of Duration Class in java.time package is used get the value of this duration in number of days. Syntax: public long toDays() Parameters: This method do not accepts any parameter. Return Value: This method returns a long value which is the number of days in this duration. Below e
    1 min read
  • Duration isZero() method in Java with Examples
    The isZero() method of Duration Class in java.time package is used to check if this duration is of 0 length ot not. It returns a boolean value depicting the same. Syntax: public boolean isZero() Parameters: This method do not accepts any parameter. Return Value: This method returns a boolean value.
    1 min read
  • Duration toHours() method in Java with Examples
    The toHours() method of Duration Class in java.time package is used get the value of this duration in number of hours. Syntax: public long toHours() Parameters: This method do not accepts any parameter. Return Value: This method returns a long value which is the number of hours in this duration. Bel
    1 min read
  • Duration getNano() method in Java with Examples
    The getNano() method of Duration Class in java.time package is used to get the nanos value of this duration. Syntax: public long getNano() Parameters: This method do not accepts any parameter. Return Value: This method returns a long value of the nanos of this duration. Below examples illustrate the
    1 min read
  • Duration toNanos() method in Java with Examples
    The toNanos() method of Duration Class in java.time package is used get the value of this duration in number of nano-seconds. Syntax: public long toNanos() Parameters: This method do not accepts any parameter. Return Value: This method returns a long value which is the number of nano-seconds in this
    1 min read
  • Duration negated() method in Java with Examples
    The negated() method of Duration Class in java.time package is used to get an immutable copy of this duration with the duration negated. Syntax: public Duration negated() Parameters: This method do not accepts any parameter. Return Value: This method returns a Duration which is an immutable copy of
    1 min read
  • Duration plus(Duration) method in Java with Examples
    The plus(Duration) 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. Syntax: public Duration plus(Duration duration) Parameters: This method accepts a parameter duration which is the duration to
    2 min read
  • Duration toMillis() method in Java with Examples
    The toMillis() method of Duration Class in java.time package is used get the value of this duration in number of milli-seconds. Syntax: public long toMillis() Parameters: This method do not accepts any parameter. Return Value: This method returns a long value which is the number of milli-seconds in
    1 min read
  • Duration toString() method in Java with Examples
    The toString() method of Duration Class in java.time package is used to get the String value of this duration. This String is in the ISO-8601 format. Syntax: public String toString() Parameters: This method do not accepts any parameter. Return Value: This method returns a String value which is the S
    1 min read
  • Duration hashCode() method in Java with Examples
    The hashCode() method of Duration Class in java.time package is used to get the hashCode value of this duration. Syntax: public int hashCode() Parameters: This method do not accepts any parameter. Return Value: This method returns an int value which is the hashCode value of this duration. Below exam
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences