Period toString() example

Description

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  ww. j av  a2s. com*/
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.





















Home »
  Java Date Time »
    java.time Reference »




Clock
DayOfWeek
Duration
Instant
LocalDate
LocalDateTime
LocalTime
Month
MonthDay
OffsetDateTime
OffsetTime
Period
Year
YearMonth
ZonedDateTime
ZoneId
ZoneOffset