|
|
Date and Time API
Author: Venkata Sudhakar
Java 8 introduced a completely new Date and Time API in the java.time package to replace the old java.util.Date and java.util.Calendar which were mutable, not thread-safe, and poorly designed. The new API is immutable, thread-safe, and follows the ISO-8601 calendar system. Key Classes: 1. LocalDate - Represents a date (year, month, day) without time. 2. LocalTime - Represents a time (hours, minutes, seconds) without date. 3. LocalDateTime - Represents a date-time without timezone. 4. ZonedDateTime - Represents a date-time with timezone. 5. Duration and Period - Represent amounts of time. The below example shows how to use the Java 8 Date and Time API.
It gives the following output,
Today: 2024-01-15
Birthday: 1990-03-15
Is leap year: true
Current time: 10:30:45.123
Meeting at: 14:30
DateTime: 2024-01-15T10:30:45.123
Next week: 2024-01-22
Last month: 2023-12-15
Age: 33 years, 10 months
Duration of meeting: 90 minutes
Formatted: 15/01/2024 10:30
Parsed: 2024-12-25
London: 10:30 GMT
Tokyo: 19:30 JST
New API vs Old API: Immutable - All operations return new objects; originals are never modified. Clear separation - Separate classes for date-only, time-only, datetime, and timezone-aware datetime. No more null checks - Methods like now(), of(), parse() have clear semantics.
|
|