Java8 - Periods and durations

Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 07:34:03 Viewed : 244


In Java is java.time package, the Period and Duration classes are used to represent amounts of time. While both classes represent a span of time, they have different use cases:

1. Period:

  • Represents a date-based amount of time, such as years, months, and days.
  • Useful for working with human-based concepts like age, tenure, or billing cycles.
java
Period period = Period.ofYears(2).plusMonths(3).plusDays(5);

Example:

java
LocalDate startDate = LocalDate.of(2020, 1, 1); LocalDate endDate = LocalDate.of(2022, 4, 6); Period period = Period.between(startDate, endDate); int years = period.getYears(); int months = period.getMonths(); int days = period.getDays(); System.out.println("Years: " + years); System.out.println("Months: " + months); System.out.println("Days: " + days);

2. Duration:

  • Represents a time-based amount of time, measured in seconds and nanoseconds.
  • Useful for precise measurements and calculations of time, such as intervals, timeouts, or durations of actions.
java
Duration duration = Duration.ofDays(5).plusHours(3).plusMinutes(30);

Example:

java
LocalTime startTime = LocalTime.of(10, 0); LocalTime endTime = LocalTime.of(15, 30); Duration duration = Duration.between(startTime, endTime); long hours = duration.toHours(); long minutes = duration.toMinutesPart(); System.out.println("Hours: " + hours); System.out.println("Minutes: " + minutes);

Combining Period and Duration:

  • It is also common to use both Period and Duration in scenarios where you need to represent both date-based and time-based intervals.
java
Period period = Period.ofMonths(2); Duration duration = Duration.ofHours(48); LocalDateTime dateTime = LocalDateTime.now(); LocalDateTime resultDateTime = dateTime.plus(period).plus(duration); System.out.println("Result DateTime: " + resultDateTime);

Key Methods:

Common Methods for Both Period and Duration:

  • ofX methods: Create instances of Period or Duration.
  • between: Calculate the amount of time between two temporal objects.
  • plus and minus: Add or subtract a Period or Duration to/from a temporal object.

Additional Methods for Period:

  • getYears, getMonths, getDays: Retrieve the individual components of a Period.

Additional Methods for Duration:

  • toDays, toHours, toMinutes, toMillis, toNanos: Convert the duration to different units.

Examples:

Period Example:

java
import java.time.*; public class PeriodExample { public static void main(String[] args) { LocalDate startDate = LocalDate.of(2020, 1, 1); LocalDate endDate = LocalDate.of(2022, 4, 6); Period period = Period.between(startDate, endDate); int years = period.getYears(); int months = period.getMonths(); int days = period.getDays(); System.out.println("Years: " + years); System.out.println("Months: " + months); System.out.println("Days: " + days); } }

Duration Example:

java
import java.time.*; public class DurationExample { public static void main(String[] args) { LocalTime startTime = LocalTime.of(10, 0); LocalTime endTime = LocalTime.of(15, 30); Duration duration = Duration.between(startTime, endTime); long hours = duration.toHours(); long minutes = duration.toMinutesPart(); System.out.println("Hours: " + hours); System.out.println("Minutes: " + minutes); } }

Combining Period and Duration Example:

java
import java.time.*; public class PeriodAndDurationExample { public static void main(String[] args) { Period period = Period.ofMonths(2); Duration duration = Duration.ofHours(48); LocalDateTime dateTime = LocalDateTime.now(); LocalDateTime resultDateTime = dateTime.plus(period).plus(duration); System.out.println("Result DateTime: " + resultDateTime); } }

In these examples, Period and Duration are used to represent date-based and time-based intervals, respectively. The methods provided by these classes offer flexibility for performing calculations involving time and date components.

Search
Related Articles

Leave a Comment: