June 29, 2022

java.time.Period Class With Examples

The java.time.Period class is part of new date and time API added in Java 8 that represents a date-based amount of time in the ISO-8601 calendar system, such as '4 years, 2 months and 10 days'.

Java Period class models a quantity or amount of time in terms of years, months and days. These three fields are always present, but may be set to zero.

Period class doesn't consider daylight savings time when added to ZonedDateTime, it will add a conceptual day, trying to maintain the local time. For example, consider adding a period of one day to 18:00 on the evening before a daylight savings gap. The Period will add the conceptual day and result in a ZonedDateTime at 18:00 the following day.

of() method in Period class

You can get an instance of Period class using methods-

  • of(int years, int months, int days)
  • ofDays(int days)
  • ofMonths(int months)
  • ofWeeks(int weeks)
  • ofYears(int years)
public class PeriodExample {
  public static void main(String[] args) {
    Period period = Period.of(2019, 10, 25);
    System.out.println("Period- " + period);
    // Year and month as 0
    period = Period.ofDays(28);
    System.out.println("Period- " + period);
    // Year and days as 0
    period = Period.ofMonths(11);
    System.out.println("Period- " + period);    
  }
}
Output
Period- P2019Y10M25D
Period- P28D
Period- P11M

Finding difference between two LocalDates using between() method

One of the important method in Period class is between() method using which you can obtain a Period consisting of the number of years, months, and days between two dates.

Java example to find difference between two LocalDate objects in terms of years, months and days.

public class PeriodExample {
  public static void main(String[] args) {
    LocalDate ld1 = LocalDate.of(2017, 7, 30);
    LocalDate ld2 = LocalDate.now();
    
    System.out.println("From Date- " + ld1);
    System.out.println("To Date- " + ld2);
    // Find difference
    Period period = Period.between(ld1, ld2);
    System.out.println("Difference between dates- " + period.getYears() + " Year(s) " 
    + period.getMonths()+ " Month(s) " + period.getDays() + " Day(s)");	
  }
}
Output
From Date- 2017-07-30
To Date- 2019-11-11
Difference between dates- 2 Year(s) 3 Month(s) 12 Day(s)

Plus or minus days, weeks, months or year from the given date

Using the plus(TemporalAmount amountToAdd) and minus(TemporalAmount amountToSubtract) methods you can add or subtract a Period from the given LocalDate. Note that Period class implements TemporalAmount.

public class PeriodExample {
  public static void main(String[] args) {
    LocalDate ld = LocalDate.of(2019, 10, 25);
    System.out.println("LocalDate- " + ld);
    System.out.println("Plus 2 Years " + ld.plus(Period.ofYears(2))); 
    System.out.println("Plus 3 Months " + ld.plus(Period.ofMonths(3))); 
    System.out.println("Plus 20 Days " + ld.plus(Period.ofDays(20))); 
    System.out.println("Plus 3 Weeks " + ld.plus(Period.ofWeeks(3))); 
  }
}
Output
LocalDate- 2019-10-25
Plus 2 Years 2021-10-25
Plus 3 Months 2020-01-25
Plus 20 Days 2019-11-14
Plus 3 Weeks 2019-11-15
public class PeriodExample {
  public static void main(String[] args) {
    LocalDate ld = LocalDate.of(2019, 10, 25);
    System.out.println("LocalDate- " + ld);
    System.out.println("Minus 2 Years " + ld.minus(Period.ofYears(2))); 
    System.out.println("Minus 3 Months " + ld.minus(Period.ofMonths(3))); 
    System.out.println("Minus 20 Days " + ld.minus(Period.ofDays(20))); 
    System.out.println("Minus 3 Weeks " + ld.minus(Period.ofWeeks(3))); 
  }
}
Output
LocalDate- 2019-10-25
Minus 2 Years 2017-10-25
Minus 3 Months 2019-07-25
Minus 20 Days 2019-10-05
Minus 3 Weeks 2019-10-04

Convert String to Period – parse() method

parse(CharSequence text)- Obtains a Period from a text string such as PnYnMnD.

In the format "PnYnMnWnD", "Y", "M", "W" and "D" represents years, months, weeks and days, accepted in upper or lower case. The suffixes must occur in order.

public class PeriodExample {
  public static void main(String[] args) {
    Period period = Period.parse("P2Y4M3W4D");
    System.out.println("Period- " + period); 
    
    period = Period.parse("P1Y7M7D");
    System.out.println("Period- " + period);
    
    period = Period.parse("-P1Y2M");
    System.out.println("Period- " + period); 
  }
}
Output
Period- P2Y4M25D
Period- P1Y7M7D
Period- P-1Y-2M

That's all for the topic java.time.Period Class With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment