Advanced Time Manipulation
Period and Duration Calculations
flowchart TD
A[Time Manipulation] --> B[Period]
A --> C[Duration]
A --> D[Temporal Adjusters]
Period Manipulation
import java.time.LocalDate;
import java.time.Period;
public class PeriodDemo {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2023, 1, 1);
LocalDate endDate = LocalDate.of(2024, 6, 15);
// Calculate period between dates
Period period = Period.between(startDate, endDate);
System.out.println("Years: " + period.getYears());
System.out.println("Months: " + period.getMonths());
System.out.println("Days: " + period.getDays());
}
}
Duration Calculations
import java.time.LocalDateTime;
import java.time.Duration;
public class DurationDemo {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.now();
LocalDateTime end = start.plusHours(5).plusMinutes(30);
Duration duration = Duration.between(start, end);
System.out.println("Hours: " + duration.toHours());
System.out.println("Minutes: " + duration.toMinutes());
}
}
Temporal Adjusters
Adjuster |
Description |
Example |
firstDayOfMonth() |
First day of current month |
date.with(TemporalAdjusters.firstDayOfMonth()) |
lastDayOfYear() |
Last day of current year |
date.with(TemporalAdjusters.lastDayOfYear()) |
nextOrSame() |
Next specified day of week |
date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY)) |
Custom Temporal Adjusters
import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class CustomTemporalAdjusterDemo {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
// Built-in adjuster
LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
// Custom adjuster
TemporalAdjuster nextWorkingDay = temporal -> {
LocalDate date = LocalDate.from(temporal);
do {
date = date.plusDays(1);
} while (date.getDayOfWeek().getValue() > 5);
return date;
};
LocalDate workingDay = today.with(nextWorkingDay);
}
}
Time Zone Conversions
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class ZoneConversionDemo {
public static void main(String[] args) {
ZonedDateTime newYorkTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
// Convert to Tokyo time
ZonedDateTime tokyoTime = newYorkTime.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
System.out.println("New York Time: " + newYorkTime);
System.out.println("Tokyo Time: " + tokyoTime);
}
}
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FormattingDemo {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
// Custom formatters
DateTimeFormatter customFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = now.format(customFormatter);
LocalDateTime parsedDate = LocalDateTime.parse(formattedDate, customFormatter);
}
}
- Use immutable time classes
- Prefer
LocalDate/LocalTime
for simple scenarios
- Handle time zones carefully
- Use appropriate formatters
LabEx Recommendation
LabEx offers comprehensive tutorials and hands-on labs to master advanced time manipulation techniques in Java, helping developers build robust time-handling applications.