TemporalAmount class

The TemporalAmount interface represents a quantity of time, such as “5 days” or “2 years and 3 months.”
It defines how a time-based amount can be added to or subtracted from a temporal object such as LocalDate, LocalDateTime, etc.

Common Used Methods

Simple Program

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.TemporalAmount;

public class TemporalAmountExample {
    public static void main(String[] args) {
        TemporalAmount amount = Period.of(1, 2, 15); // 1 year, 2 months, 15 days
        LocalDate today = LocalDate.now();

        LocalDate futureDate = today.plus(amount);
        LocalDate pastDate = today.minus(amount);

        System.out.println("Today: " + today);
        System.out.println("After TemporalAmount: " + futureDate);
        System.out.println("Before TemporalAmount: " + pastDate);
    }
}

Problem Statement

LotusJavaPrince and Mahesh are building a subscription system where:

  • Users get a free trial of 1 month and 15 days.
  • Subscription renewals add 1 year to the current date.
  • The system should display both trial end and renewal end dates dynamically.
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.TemporalAmount;

public class SubscriptionManager {
    public static void main(String[] args) {
        System.out.println("Subscription System - Designed by LotusJavaPrince & Mahesh");

        LocalDate today = LocalDate.now();

        TemporalAmount trialPeriod = Period.ofMonths(1).plusDays(15);
        TemporalAmount renewalPeriod = Period.ofYears(1);

        LocalDate trialEndDate = today.plus(trialPeriod);
        LocalDate renewalEndDate = trialEndDate.plus(renewalPeriod);

        System.out.println("Today's Date: " + today);
        System.out.println("Trial Ends On: " + trialEndDate);
        System.out.println("Renewal Ends On: " + renewalEndDate);
    }
}
/*
Subscription System - Designed by LotusJavaPrince & Mahesh
Today's Date: 2025-05-22
Trial Ends On: 2025-07-06
Renewal Ends On: 2026-07-06
*/

TemporalAmount is a flexible and abstract representation of a time period.

  • It is primarily implemented by Period and Duration.
  • Ideal for adding or subtracting dynamic time values to temporal objects.
  • Helps standardize temporal logic in subscription systems, scheduling, licensing, and billing applications.
Scroll to Top