Skip to content

Commit f7ffac6

Browse files
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

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Why New Date-Time API (java.time)?
2+
3+
1. Problems with Old API (java.util.Date, java.util.Calendar):
4+
- Mutable: `Date` and `Calendar` objects can be modified → not thread-safe.
5+
- Poor Design: Months start from 0 (January = 0), causing confusion.
6+
- Ambiguous: `java.util.Date` stores both date & time, but names like `getYear()`, `getMonth()` are misleading.
7+
- Time Zone Issues: Very limited and error-prone handling of zones.
8+
- Not Immutable: Harder to use in multi-threaded apps safely.
9+
10+
2. Java 8 Date-Time API Advantages:
11+
- Immutable → Once created, cannot be changed (thread-safe).
12+
- Clear separation of concerns:
13+
✔ `LocalDate` → Date only (no time).
14+
✔ `LocalTime` → Time only (no date).
15+
✔ `LocalDateTime` → Date + Time (no zone).
16+
✔ `ZonedDateTime` → Date + Time + Zone.
17+
- Better formatting and parsing with `DateTimeFormatter`.
18+
- Inspired by the popular Joda-Time library.
19+
20+
=========================
21+
Core Classes
22+
=========================
23+
24+
1. LocalDate:
25+
- Represents only a calendar date (year, month, day).
26+
- Example: `LocalDate today = LocalDate.now();`
27+
- Immutable → You cannot change the date directly.
28+
- Instead, use methods like:
29+
- `plusDays(5)`, `minusMonths(2)`, `withYear(2020)` → returns new objects.
30+
31+
2. LocalTime:
32+
- Represents only the time (hour, minute, second, nanosecond).
33+
- Example: `LocalTime now = LocalTime.now();`
34+
- No date, no time zone.
35+
36+
3. LocalDateTime:
37+
- Represents date + time, down to nanoseconds.
38+
- Example: `LocalDateTime dt = LocalDateTime.now();`
39+
- Useful for timestamps without zones.
40+
- Still immutable → all modifications return new instances.
41+
42+
=========================
43+
Old API vs New API
44+
=========================
45+
46+
Old (java.util.Date):
47+
----------------------
48+
Date d = new Date();
49+
// Stores date-time internally as milliseconds since 1 Jan 1970 (epoch).
50+
51+
✔ Can modify:
52+
d.setTime(1234567890L);
53+
d.setYear(2025); // Deprecated, mutable, error-prone.
54+
55+
New (java.time):
56+
----------------------
57+
LocalDate date = LocalDate.now();
58+
LocalDate newDate = date.plusDays(5); // Creates a NEW object, original unchanged.
59+
60+
✔ Immutable and thread-safe.
61+
✔ Easier to read, maintain, and extend.
62+
63+
✔ Old API (`Date`, `Calendar`) → mutable, buggy, confusing.
64+
✔ New API (`LocalDate`, `LocalTime`, `LocalDateTime`) → immutable, clean, safe.
65+
✔ Separation of Date, Time, and DateTime = better clarity.
66+
✔ Use `Period` (date-based) or `Duration` (time-based) for differences.

0 commit comments

Comments
 (0)