Practical Time Manipulation
Real-world Time Manipulation Scenarios
Calculating Age and Duration
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class AgeCalculator {
public static void main(String[] args) {
LocalDate birthDate = LocalDate.of(1990, 5, 15);
LocalDate currentDate = LocalDate.now();
// Calculate exact age
Period age = Period.between(birthDate, currentDate);
long exactYears = ChronoUnit.YEARS.between(birthDate, currentDate);
System.out.println("Age: " + age.getYears() + " years");
System.out.println("Exact Years: " + exactYears);
}
}
Time-based Validation and Filtering
graph LR
A[Input Dates] --> B{Validation}
B --> |Valid| C[Process]
B --> |Invalid| D[Reject]
Date Range Validation
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
public class DateRangeFilter {
public static void main(String[] args) {
List<LocalDate> dates = List.of(
LocalDate.of(2023, 1, 15),
LocalDate.of(2023, 5, 20),
LocalDate.of(2023, 8, 10)
);
LocalDate startDate = LocalDate.of(2023, 2, 1);
LocalDate endDate = LocalDate.of(2023, 7, 31);
// Filter dates within a specific range
List<LocalDate> filteredDates = dates.stream()
.filter(date -> !date.isBefore(startDate) && !date.isAfter(endDate))
.collect(Collectors.toList());
System.out.println("Filtered Dates: " + filteredDates);
}
}
Advanced Time Manipulation Techniques
Working with Business Days
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class BusinessDayCalculator {
public static LocalDate nextBusinessDay(LocalDate date) {
LocalDate nextDay = date.plusDays(1);
while (isWeekend(nextDay)) {
nextDay = nextDay.plusDays(1);
}
return nextDay;
}
private static boolean isWeekend(LocalDate date) {
return date.getDayOfWeek() == DayOfWeek.SATURDAY ||
date.getDayOfWeek() == DayOfWeek.SUNDAY;
}
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate nextBusinessDay = nextBusinessDay(today);
System.out.println("Today: " + today);
System.out.println("Next Business Day: " + nextBusinessDay);
}
}
Time Manipulation Patterns
Scenario |
Technique |
Use Case |
Age Calculation |
Period.between() |
Determining exact age |
Date Filtering |
Stream API |
Selecting dates in range |
Business Days |
Custom Logic |
Skipping weekends |
- Use immutable date-time objects
- Leverage Stream API for filtering
- Minimize complex date calculations
Error Handling in Time Manipulation
import java.time.DateTimeException;
import java.time.LocalDate;
public class SafeDateHandling {
public static LocalDate safeParseDate(String dateString) {
try {
return LocalDate.parse(dateString);
} catch (DateTimeException e) {
System.err.println("Invalid date format: " + e.getMessage());
return LocalDate.now(); // Fallback to current date
}
}
public static void main(String[] args) {
String invalidDate = "2023-02-30";
LocalDate parsedDate = safeParseDate(invalidDate);
}
}
Practical Applications in LabEx Projects
- Event scheduling
- User activity tracking
- Compliance and reporting
- Time-sensitive data processing