Date & Time API
LocalTime
Manage timezone-independent times and time intervals.
Interview: Tests time arithmetic, resolution limits (nanosecond precision), and conversions.
LocalTime represents a timezone-independent time (often viewed as hour-minute-second-nanosecond). It has no date or timezone context.
Core Idea
LocalTime models a wall-clock time like '09:30:00' without timezone offsets.
Why It Matters
Used to define scheduled execution checkpoints (e.g. run a batch job daily at 2 AM).
Interview Lens
Focuses on resolution limits (nanoseconds) and performing time comparisons.
Core API Operations
- Creation:
LocalTime.of(14, 30)(2:30 PM) orLocalTime.parse("14:30:00"). - Arithmetic:
plusHours(2),minusMinutes(30). - Truncation:
truncatedTo(ChronoUnit.MINUTES)discards seconds/nanoseconds.
Code Walkthrough
This program shows how to create and adjust LocalTime values.
Interview-Relevant Information
Q: What is the maximum resolution of LocalTime in Java?
Answer: LocalTime supports nanosecond resolution (10^-9 seconds). It is backed by a long representing nanoseconds within a day, ensuring high precision.
Quick Checklist
What is the resolution of LocalTime? How do you drop seconds from a time instance? If yes, you understand LocalTime.
Use Cases
Defining opening and closing business hour limits.
Scheduling background tasks to run at a specific time of day.
Common Mistakes
Passing invalid hour parameters (outside 0-23 range), throwing DateTimeException.
Expecting LocalTime to represent timezone offsets.