Java Data Type How to - Parse Period from string








Question

We would like to know how to parse Period from string.

Answer

import java.time.Duration;
import java.time.Period;
/*from w  ww  .  jav  a 2  s.  co m*/
public class Main {

  public static void main(String[] args) {
    System.out.println(Period.parse("P3Y6M4D"));
    System.out.println(Duration.parse("PT12H30M5S"));
  }
}

The code above generates the following result.