Java Data Type How to - Create Period of time








Question

We would like to know how to create Period of time.

Answer

import java.time.Period;
//from w w w.  ja  v a2  s.com
public class Main {

  public static void main(String[] args) {
    Period p = Period.ofDays(30);
    System.out.println(p);

    Period p1 = Period.ofMonths(12);
    System.out.println(p1);

    Period p2 = Period.ofWeeks(11);
    System.out.println(p2);

    Period p3 = Period.ofYears(50); // years, months, days
    System.out.println(p3);
  }
}

The code above generates the following result.