Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
java.time.Period Class in Java
Next article icon

java.time.Period Class in Java

Last Updated : 04 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The Period Class in Java class obtains a quantity or amount of time in terms of years, months and days. The time obtained is a date-based amount of time in the ISO-8601 calendar system, such as '4 years, 5 months, and 6 days. The units which are supported for a period are YEARS, MONTHS, and days. All these three fields are always present but may be set to zero.

Syntax: Class declaration

public final class Period  extends Object  implements ChronoPeriod, Serializable

Below all the methods with the action performed by them are as follows in the tabular format are as follows:

MethodDescription
addTo(Temporal temporal)This method adds this period to the specified temporal object.
between(LocalDate startDateInclusive, LocalDate endDateExclusive)This method obtains a Period consisting of the number of years, months, and days between two dates.
equals(Object obj)This method checks if this period is equal to another period.
from(TemporalAmount amount)This method obtains an instance of Period from a temporal amount.
get(TemporalUnit unit)This method gets the value of the requested unit.
getChronology()This method gets the chronology of this period, which is the ISO calendar system.
getDays()This method gets the number of days of this period.
getMonths()This method gets the number of months of this period.
getUnits()This method gets the set of units supported by this period.
getYears()This method gets the number of years of this period.
hashCode()This method returns a hash code for this period.
isNegative()This method checks if any of the three units of this period are negative.
isZero()This method checks if all three units of this period are zero.
minus(TemporalAmount amountToSubtract)This method returns a copy of this period with the specified period subtracted.
minusDays(long daysToSubtract)This method returns a copy of this period with the specified days subtracted.
minusMonths(long monthsToSubtract)This method returns a copy of this period with the specified months subtracted.
minusYears(long yearsToSubtract)This method returns a copy of this period with the specified years subtracted.
multipliedBy(int scalar)This method returns a new instance with each element in this period multiplied by the specified scalar.
negated()This method returns a new instance with each amount in this period negated.
normalized()This method returns a copy of this period with the years and months normalized.
of(int years, int months, int days)This method obtains a Period representing a number of years, months, and days.
ofDays(int days)This method obtains a Period representing a number of days.
ofMonths(int months)This method obtains a Period representing a number of months.
ofWeeks(int weeks)This method obtains a Period representing a number of weeks.
ofYears(int years)This method obtains a Period representing a number of years.
parse(CharSequence text)This method obtains a Period from a text string such as PnYnMnD.
plus(TemporalAmount amountToAdd)This method returns a copy of this period with the specified period added.
plusDays(long daysToAdd)This method returns a copy of this period with the specified days added.
plusMonths(long monthsToAdd)This method returns a copy of this period with the specified months added.
plusYears(long yearsToAdd)This method returns a copy of this period with the specified years added.
subtractFrom(Temporal temporal)This method subtracts this period from the specified temporal object.
toString()This method outputs this period as a String, such as P6Y3M1D.
toTotalMonths()This method gets the total number of months in this period.
withDays(int days)This method returns a copy of this period with the specified amount of days. 
withMonths(int months)This method returns a copy of this period with the specified amount of months.
withYears(int years)This method returns a copy of this period with the specified amount of years.

Let us implement a few of the methods of this class.

Implementation:

Example 1

Java
// Java program to illustrate Period class // demonstrate the methods of this class // Methods - minus() and ofMonths()  // Importing Period class from // java.time package import java.time.Period;  // Main class public class GFG {     // Main driver method     public static void main(String[] args)     {         // Obtaining period representing number of months         // using of months() method by         // creating object of period class         Period p1 = Period.ofMonths(6);          // minus() will return a copy of this period         // with the specified period subtracted.         Period p2 = p1.minus(Period.ofMonths(2));          // Print and display on the console         System.out.println(p2);     } } 

Output
P4M

Let us take another example in order to discuss more methods namely as follows:

Method 1: ofDays() method of this class is used to obtain a period from the given number of Days as a parameter. 

Syntax:

public static Period ofDays(int numberOfDays)

Parameters: This method accepts a single parameter number of days which is the number of Days to be parsed into a Period object.

Returns: This function returns the period which is the Period object parsed with the given number of Days.

Method 2: addTo() method of this class adds this Period to the specific temporal object.

Syntax:

public Temporal addTo(Temporal temporal)

Parameters: The temporal object to adjust should not be null.

Return Type: An object of the same type with the adjustment made.

Exceptions: DateTime and Arithmetic exceptions are thrown by this method.

Example 2

Java
// Java program to illustrate Period class // demonstrate the methods of this class // Methods like ofDays() and addTo()  // Importing all classes from java.time package import java.time.*; import java.time.temporal.Temporal;  // Main class public class GFG {      // Main driver method     public static void main(String[] args)     {         // Getting a period representing number of days         // using ofDays() method         Period p = Period.ofDays(24);          // Adding this period to the         // temporal object i.e. temp         Temporal temp = p.addTo(LocalDate.now());          // Print and display on the console         System.out.println(temp);     } } 

Output
2021-03-29

Next Article
java.time.Period Class in Java

S

surbhityagi15
Improve
Article Tags :
  • Java
  • Java-time package
  • Java-Period
Practice Tags :
  • Java

Similar Reads

    java.time.LocalDate Class in Java
    Java is the most popular programming language and widely used programming language. Java is used in all kind of application like as mobile application, desktop application, web application. In this Java java.time.LocalDate class is imported which represents to display the current date. java.time: It
    4 min read
    java.time.OffsetTime Class in Java
    Java OffsetTime class is an immutable date-time object that represents a time, often viewed as hour-minute-second offset. OffsetTime class represents a time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 18:30:45+08:00, often viewed as an hour-minute-second-offset. This c
    7 min read
    java.time.LocalTime Class in Java
    Java is the most popular programming language and widely used programming language. Java is used in all kinds of applications like mobile applications, desktop applications, web applications. As in Java, java.time.LocalTime class represents time, which is viewed as hour-minute-second. This class is
    5 min read
    java.time.Year Class in Java
    The java.time.Year class represents a year in the ISO-8601 calendar system, such as 2021. Year is an immutable date-time object that represents a year. This class does not store or represent a month, day, time, or time-zone. The years represented by this class follow the proleptic numbering system t
    8 min read
    java.time.OffsetDateTime Class in Java
    Java is the most popular programming language and widely used programming language. Java is used in all kind of application like mobile application, desktop application, web application. In this Java java.time.OffsetDate, Time class is imported which is an immutable representation of a date-time wit
    4 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