The TemporalUnit interface represents a unit of time, such as days, hours, or months, and defines how these units can be added to or between temporal objects (LocalDate, LocalDateTime, etc.).It works with temporal-based classes to quantify time in a meaningful way.
Common Used Methods

Simple Program
import java.time.LocalDateTime;
import java.time.temporal.TemporalUnit;
import java.time.temporal.ChronoUnit;
public class TemporalUnitExample {
public static void main(String[] args) {
TemporalUnit unit = ChronoUnit.DAYS;
LocalDateTime now = LocalDateTime.now();
LocalDateTime future = unit.addTo(now, 10);
System.out.println("Current Time: " + now);
System.out.println("After 10 days: " + future);
}
}Problem Statement
LotusJavaPrince and Mahesh are developing a training scheduler where:
- A course spans 90 days from the start date.
- The system must calculate the end date of the course and the days remaining for the user if they log in today.
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
public class TrainingScheduler {
public static void main(String[] args) {
System.out.println("Training Scheduler - Developed by LotusJavaPrince and Mahesh");
LocalDate startDate = LocalDate.of(2025, 3, 1);
TemporalUnit unit = ChronoUnit.DAYS;
long courseDuration = 90;
LocalDate endDate = (LocalDate) unit.addTo(startDate, courseDuration);
LocalDate today = LocalDate.now();
long remainingDays = unit.between(today, endDate);
System.out.println("Course Start Date: " + startDate);
System.out.println("Course End Date: " + endDate);
System.out.println("Today's Date: " + today);
if (remainingDays >= 0) {
System.out.println("Days Remaining: " + remainingDays);
} else {
System.out.println("The course has ended.");
}
}
}
/*
Training Scheduler - Developed by LotusJavaPrince and Mahesh
Course Start Date: 2025-03-01
Course End Date: 2025-05-30
Today's Date: 2025-05-22
Days Remaining: 8
*/TemporalUnit is a standardized interface for defining units of time such as days, weeks, hours, etc.
- It is heavily used in ChronoUnit, which provides predefined units.
- Excellent for measuring time gaps, scheduling, and adding durations to dates and times.
