Commit 6aa6105
committed
feat(datetime-api): add ChronoFieldDemo to showcase field-based access to LocalDateTime
What
- Introduced ChronoFieldDemo class under DateandTimeAPI package.
- Demonstrates retrieving specific temporal fields from LocalDateTime using ChronoField.
- Fields shown in example:
- DAY_OF_MONTH → numeric day in current month.
- AMPM_OF_DAY → 0 for AM, 1 for PM.
- YEAR_OF_ERA → year within current era (e.g., 2025).
- YEAR → absolute year (can be negative for BCE).
Why
- Provides a hands-on example of the java.time.temporal.ChronoField API.
- Helps learners understand how ChronoField can extract granular components of a temporal object.
- Reinforces that ChronoField values are integer-based representations useful for low-level date/time manipulation.
How to use
- Run ChronoFieldDemo main().
- The output prints current system date-time values, field by field.
Real-life applications
- Logging or auditing where only specific date parts (e.g., day, year, or AM/PM) are required.
- Custom calendar and scheduling software where calculations depend on fields rather than complete objects.
- Financial apps generating reports by month/day/year without needing entire LocalDateTime objects.
- Internationalization systems where AM/PM or era-specific year (e.g., BCE/CE) matters.
Notes
- Unlike direct getters (dt.getDayOfMonth()), ChronoField provides a uniform field-based access mechanism.
- Using ChronoField with get() throws UnsupportedTemporalTypeException if the field is not supported by
the temporal type.
- Consider ChronoUnit for measuring differences, while ChronoField is for extraction.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 05afff4 commit 6aa6105
File tree
1 file changed
+1
-0
lines changed- Section26DateandTimeAPI/src/DateandTimeAPI
1 file changed
+1
-0
lines changedLines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
0 commit comments