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:
javaPeriod period = Period.ofYears(2).plusMonths(3).plusDays(5);
javaLocalDate 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);
javaDuration duration = Duration.ofDays(5).plusHours(3).plusMinutes(30);
javaLocalTime 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);
Period
and Duration
in scenarios where you need to represent both date-based and time-based intervals.javaPeriod 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);
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.Period
:getYears
, getMonths
, getDays
: Retrieve the individual components of a Period
.Duration
:toDays
, toHours
, toMinutes
, toMillis
, toNanos
: Convert the duration to different units.javaimport 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);
}
}
javaimport 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);
}
}
javaimport 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.