Commit a362ace
committed
feat(datetime-api): add DateDemo showcasing legacy Date vs java.time API
What
- Introduced DateDemo under DateandTimeAPI package.
- Demonstrates usage of both legacy java.util.Date and modern java.time API.
- Key operations shown:
- Printing current date/time using Date.
- Modifying time via setHours() (legacy, deprecated).
- Creating LocalDate with different factories:
- now() with system default clock.
- now(ZoneId) for specific time zones.
- of(year, month, day) for explicit date.
- ofEpochDay() using epoch days.
- parse("yyyy-MM-dd") for ISO-8601 parsing.
- Performing date arithmetic with plusMonths().
- Using LocalTime and LocalDateTime to retrieve and manipulate times.
- Accessing LocalDateTime components (nano, day of month).
Why
- Highlights contrast between mutable, deprecated java.util.Date and immutable, thread-safe java.time classes.
- Provides hands-on examples for creating, parsing, and manipulating dates and times.
- Reinforces why java.time is preferred in modern applications.
How to use
- Run DateDemo.
- Output will display:
1. Current date/time using legacy Date.
2. Modified Date after setHours().
3. Current LocalDate in different ways (default, zone-based, explicit).
4. Dates created from epoch days and parsing.
5. LocalTime with subtraction of hours.
6. LocalDateTime with nano precision and day-of-month extraction.
Real-life applications
- Parsing user-input dates in forms (LocalDate.parse).
- Scheduling events in specific time zones (LocalDate.now(ZoneId)).
- Handling recurring billing cycles or reminders (plusMonths()).
- Capturing precise timestamps for logging/auditing (LocalDateTime.now()).
- Migrating legacy Date-based code to modern java.time API.
Notes
- java.util.Date is mutable and has deprecated methods (`setHours()`).
- LocalDate, LocalTime, LocalDateTime are immutable and safe for concurrency.
- java.time supports time zones via ZoneId and ZonedDateTime.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 57c345d commit a362ace
1 file changed
+8
-7
lines changedLines changed: 8 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | | - | |
6 | 3 | | |
7 | 4 | | |
8 | 5 | | |
9 | 6 | | |
10 | | - | |
| 7 | + | |
| 8 | + | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
| 12 | + | |
14 | 13 | | |
15 | 14 | | |
16 | | - | |
| 15 | + | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
53 | | - | |
| 54 | + | |
0 commit comments