Skip to content

Commit 6aa6105

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

1 file changed

+1
-0
lines changed

Section26DateandTimeAPI/src/DateandTimeAPI/ChronoFieldDemo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public static void main(String[] args) {
1010
System.out.println(dt.get(ChronoField.DAY_OF_MONTH));
1111
System.out.println(dt.get(ChronoField.AMPM_OF_DAY));
1212
System.out.println(dt.get(ChronoField.YEAR_OF_ERA));
13+
System.out.println(dt.get(ChronoField.YEAR));
1314
}
1415
}

0 commit comments

Comments
 (0)