Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
tostring
period

What is Period.toString() 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.

Overview

The toString() method is defined in the Period class. It is used to get the string representation of the Period object. The string will be of ISO-8601 period format i.e PyYmMdD where y indicates the value in years, m indicates the value in months, and d indicates the value in days.

Importing Period class

The Period class is defined in the java.time package. To import the Period class check the following import statement:

import java.time.Period;
Importing Period class

Syntax

Let’s view the syntax of the function:

public String toString()
toString syntax

Parameters

The method accepts no parameters.

Return value

This method returns the string representation of the Period object.

Code

Let’s view the working of the function:

import java.time.Period;
public class Main{
public static void main(String[] args) {
int numYears = 5;
int numMonths = 10;
int numDays = 13;
Period period = Period.of(numYears, numMonths, numDays);
System.out.println("Period object - " + period.toString());
}
}

In the above code, we printed the string representation of the Period object to the console.

RELATED TAGS

java
communitycreator
tostring
period

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