Date & Time API
LocalDateTime
Combine dates and times without timezone offsets.
Interview: Tests combining LocalDate and LocalTime, and converting them to ZonedDateTime or Instant.
LocalDateTime represents a timezone-independent date-time object, combining LocalDate and LocalTime (e.g. '2026-06-13T14:30:00').
Core Idea
LocalDateTime couples a calendar date with a wall-clock time, but does not represent a specific moment on the global timeline.
Why It Matters
Ideal for representing event details (e.g. 'class starts at 9 AM on July 10th') that apply locally regardless of the viewer's location.
Interview Lens
Tests how to convert a LocalDateTime into a specific Instant by applying a timezone offset.
Combining and Splitting
- Combination:
LocalDate.atTime(LocalTime)orLocalDateTime.of(date, time). - Splitting: Extract components using
toLocalDate()andtoLocalTime(). - Conversion: To convert to an Instant, you must supply a timezone:
localDateTime.atZone(ZoneId).toInstant().
Code Walkthrough
This program shows how to combine date/time variables and convert them.
Interview-Relevant Information
Q: Can you compare two LocalDateTime instances to determine which happened first on the global timeline?
Answer: No, not safely if they occurred in different locations. Because LocalDateTime lacks timezone context, 2:00 PM in London and 2:00 PM in New York will show as equal, despite happening 5 hours apart. To compare global moments, you must convert them to Instant or ZonedDateTime first.
Quick Checklist
How do you split a LocalDateTime? Can you compare two local date-times across zones? If yes, you understand LocalDateTime.
Use Cases
Defining calendar schedules inside course scheduling catalogs.
Logging local machine diagnostic checkpoints.
Common Mistakes
Using LocalDateTime to store transaction timestamps (always use Instant or OffsetDateTime to preserve the global point in time).
Assuming a LocalDateTime has a default timezone (it is completely zone-blind).