Date and Time APIs
Overview of Java Time APIs
Java provides comprehensive APIs for handling dates, times, and time-related operations. The java.time
package offers a rich set of classes and methods for sophisticated time management.
Key Date and Time APIs
public class DateTimeFormatting {
public static void main(String[] args) {
// Parsing date from string
LocalDate parsedDate = LocalDate.parse("2023-06-15");
// Custom formatting
DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formattedDate = LocalDate.now().format(customFormatter);
System.out.println("Formatted Date: " + formattedDate);
// Parsing with custom format
LocalDate customParsedDate = LocalDate.parse("15/06/2023", customFormatter);
System.out.println("Custom Parsed Date: " + customParsedDate);
}
}
2. Date Calculations
public class DateCalculations {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
// Adding days
LocalDate futureDate = today.plusDays(30);
// Subtracting months
LocalDate pastDate = today.minusMonths(2);
// Comparing dates
boolean isBefore = today.isBefore(futureDate);
boolean isAfter = today.isAfter(pastDate);
System.out.println("Future Date: " + futureDate);
System.out.println("Past Date: " + pastDate);
System.out.println("Is Before Future: " + isBefore);
System.out.println("Is After Past: " + isAfter);
}
}
Time Zone Operations
graph TD
A[Local Time] --> B[Convert to Different Time Zones]
B --> C[ZonedDateTime]
C --> D[Instant]
D --> E[Universal Timestamp]
Time Zone Conversion Example
public class TimeZoneOperations {
public static void main(String[] args) {
// Current time in system default zone
ZonedDateTime systemTime = ZonedDateTime.now();
// Convert to specific time zone
ZonedDateTime tokyoTime = systemTime.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
// Convert to different time zone
ZonedDateTime newYorkTime = systemTime.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println("System Time: " + systemTime);
System.out.println("Tokyo Time: " + tokyoTime);
System.out.println("New York Time: " + newYorkTime);
}
}
Comprehensive API Comparison
API |
Use Case |
Key Methods |
LocalDate |
Date without time |
plusDays() , minusMonths() |
LocalTime |
Time without date |
plusHours() , minusMinutes() |
LocalDateTime |
Date and time |
atZone() , toLocalDate() |
ZonedDateTime |
Date, time, and zone |
withZoneSameInstant() |
Instant |
Machine timestamp |
now() , ofEpochSecond() |
Advanced Time Manipulations
public class AdvancedTimeManipulation {
public static void main(String[] args) {
// Period calculation
LocalDate startDate = LocalDate.of(2023, 1, 1);
LocalDate endDate = LocalDate.of(2023, 12, 31);
Period period = Period.between(startDate, endDate);
// Duration calculation
LocalTime startTime = LocalTime.of(10, 0);
LocalTime endTime = LocalTime.of(15, 30);
Duration duration = Duration.between(startTime, endTime);
System.out.println("Period: " + period);
System.out.println("Duration: " + duration);
}
}
LabEx Learning Tip
LabEx recommends practicing these APIs through interactive coding exercises to build muscle memory and deep understanding.
Best Practices
- Use appropriate time classes for specific scenarios
- Always consider time zones in global applications
- Prefer immutable time classes
- Use built-in formatting and parsing methods