Java Period and Duration Examples

1. Introduction

In this tutorial, we will explore the usage of Java's Period and Duration APIs with detailed examples. These APIs are part of Java's date-time library and are crucial for handling precise date and time calculations in Java applications.

Key Points

1. Period is used to represent a period of time in terms of years, months, and days.

2. Duration is used to represent a period of time in terms of seconds and nanoseconds.

3. Methods to create, manipulate, and calculate differences using both Period and Duration.

2. Program Steps

1. Create Period and Duration objects using various methods.

2. Demonstrate adding and subtracting time to/from these objects.

3. Show how to calculate the difference between dates and times.

4. Convert Duration to a total number of days, hours, and minutes.

3. Code Program

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class Main {

    public static void main(String[] args) {
        // Create Period examples
        Period tenDays = Period.ofDays(10);
        System.out.println("Period of 10 days: " + tenDays);

        Period threeWeeks = Period.ofWeeks(3);
        System.out.println("Period of 3 weeks: " + threeWeeks);

        Period complexPeriod = Period.of(1, 2, 15);
        System.out.println("Complex period: " + complexPeriod);

        // Create Duration examples
        Duration threeMinutes = Duration.ofMinutes(3);
        System.out.println("Duration of 3 minutes: " + threeMinutes);

        Duration twoHours = Duration.ofHours(2);
        System.out.println("Duration of 2 hours: " + twoHours);

        // Add to period and duration
        Period addedPeriod = tenDays.plus(threeWeeks);
        System.out.println("Added period: " + addedPeriod);

        Duration doubledDuration = threeMinutes.multipliedBy(2);
        System.out.println("Doubled duration: " + doubledDuration);

        // Calculate differences
        LocalDate date1 = LocalDate.of(2020, 1, 1);
        LocalDate date2 = LocalDate.of(2020, 1, 15);
        Period periodBetween = Period.between(date1, date2);
        System.out.println("Period between dates: " + periodBetween);

        LocalDateTime time1 = LocalDateTime.of(2020, 1, 1, 12, 0);
        LocalDateTime time2 = LocalDateTime.of(2020, 1, 1, 14, 30);
        Duration durationBetween = Duration.between(time1, time2);
        System.out.println("Duration between times: " + durationBetween);

        // Total days in duration
        long daysInDuration = durationBetween.toDays();
        System.out.println("Total days in duration: " + daysInDuration);
    }
}

Output:

Period of 10 days: P10D
Period of 3 weeks: P21D
Complex period: P1Y2M15D
Duration of 3 minutes: PT3M
Duration of 2 hours: PT2H
Added period: P1M1D
Doubled duration: PT6M
Period between dates: P14D
Duration between times: PT2H30M
Total days in duration: 0

Explanation:

1. Period.ofDays(10) creates a period of 10 days.

2. Period.ofWeeks(3) translates to a period of 21 days.

3. Period.of(1, 2, 15) represents 1 year, 2 months, and 15 days.

4. Duration.ofMinutes(3) and Duration.ofHours(2) demonstrate simple duration creation.

5. Adding periods and multiplying durations demonstrate how to manipulate time amounts.

6. Period.between(date1, date2) calculates the period between two dates.

7. Duration.between(time1, time2) calculates the duration between two times.

8. durationBetween.toDays() shows how to convert a duration into total days, emphasizing that this particular duration does not span a complete day.

Comments