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.
Period classThe Period class is defined in the java.time package. To import the Period class check the following import statement:
import java.time.Period;
Let’s view the syntax of the function:
public String toString()
The method accepts no parameters.
This method returns the string representation of the Period object.
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.