What is Period.toString() in Java?
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;
Syntax
Let’s view the syntax of the function:
public String toString()
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.