Java Date Time - Period toString() example








Period toString() outputs this period as a String, such as P6Y3M1D. The output will be in the ISO-8601 period format.

A zero period will be represented as zero days, 'P0D'.

Syntax

toString has the following syntax.

public String toString()

Example

The following example shows how to use toString.

import java.time.Period;
// w  w  w .  j  a  va  2 s  .  co m
public class Main {
  public static void main(String[] args) {
    Period p = Period.ofDays(12);

    System.out.println(p.toString());

  }
}

The code above generates the following result.