Open In App

ChronoPeriod toString() method in Java with Examples

Last Updated : 24 Jun, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The toString() method of ChronoPeriod interface in Java is used to return a String representation of this ChronoPeriod.

Syntax:

ChronoPeriod toString()

Parameters: This method does not accepts any parameter.

Return Value: This method returns String representation of this

Below program illustrates the above method:

Program 1:




// Java code to show the toString() function
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
  
public class ChronoPeriodClass {
  
    // Function to negate given periods
    static void printString(ChronoPeriod p1)
    {
  
        System.out.println(p1.toString());
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Defining period
        int year = 4;
        int months = 11;
        int days = 10;
        ChronoPeriod p1 = Period.of(year, months, days);
  
        printString(p1);
    }
}


Output:

P4Y11M10D

Program 2:




// Java code to show the toString() function
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
  
public class ChronoPeriodClass {
  
    // Function to negate given periods
    static void printString(ChronoPeriod p1)
    {
  
        System.out.println(p1.toString());
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Defining period
        int year = -4;
        int months = -11;
        int days = -10;
        ChronoPeriod p1 = Period.of(year, months, days);
  
        printString(p1);
    }
}


Output:

P-4Y-11M-10D

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#toString–



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads