Commit f7ffac6
committed
docs(datetime-api): explain advantages of java.time over legacy Date/Calendar
What
- Added documentation outlining problems with legacy APIs (`java.util.Date`, `java.util.Calendar`) and how `java.time` solves them.
- Highlighted key issues with old APIs:
- Mutability → not thread-safe.
- Poor design (e.g., months starting at 0).
- Ambiguous and misleading method names.
- Weak, error-prone time zone handling.
- Explained Java 8+ improvements:
- Immutable, thread-safe classes.
- Clear separation of concerns (`LocalDate`, `LocalTime`, `LocalDateTime`, `ZonedDateTime`).
- Powerful formatting & parsing via `DateTimeFormatter`.
- Inspired by Joda-Time library.
- Provided detailed notes on core classes:
- `LocalDate` → only date.
- `LocalTime` → only time.
- `LocalDateTime` → date + time (no zone).
- Emphasis on immutability and return of new instances for modifications.
- Added Old vs New API comparison:
- `Date` mutable vs `LocalDate` immutable.
- Deprecated setters vs fluent methods (`plusDays`, `minusMonths`, `withYear`).
Why
- To help developers migrate from legacy APIs to modern `java.time`.
- Clarifies pitfalls of using `Date`/`Calendar` in multi-threaded environments.
- Shows clear benefits in readability, maintainability, and safety with new API.
How to use
- Prefer `java.time` classes for all new development.
- Use `LocalDate`/`LocalTime`/`LocalDateTime` for date & time handling.
- Use `ZonedDateTime` for timezone-aware applications.
- Use `Period` for date differences and `Duration` for time differences.
Real-life applications
- Safer handling of financial transactions with immutable timestamps.
- Accurate time zone conversions for global applications.
- Cleaner and more readable APIs for UIs, reporting, and scheduling systems.
- Thread-safe logging and auditing with precise timestamps.
Notes
- Legacy `Date` and `Calendar` still exist for backward compatibility.
- For new projects, always prefer `java.time` package.
- `java.time` classes integrate well with JDBC, JSON APIs, and frameworks like Spring.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 3df1606 commit f7ffac6
1 file changed
+66
-0
lines changedLines changed: 66 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
0 commit comments