Skip to content

Commit 314f06a

Browse files
committed
feat(datetime-api): add DateTimeFormatDemo showcasing custom date-time formatting
What - Introduced DateTimeFormatDemo under DateandTimeAPI package. - Demonstrates use of DateTimeFormatter with LocalDateTime and ZonedDateTime. - Added multiple formatting patterns: - "dd/MM/yyyy" → formats date only (day/month/year). - "dd/MM/yyyy hh:mm:ss" → adds time in 12-hour format. - "dd/MM/yyyy hh:mm:ss z" → includes time zone abbreviation using ZonedDateTime. Why - Provides clear examples of formatting temporal objects using patterns. - Shows how different symbols (y, M, d, h, H, m, s, S, z, Z) control output formatting. - Reinforces distinction between LocalDateTime (no timezone) and ZonedDateTime (timezone aware). How to use - Run DateTimeFormatDemo. - Output will display: 1. Current system date in dd/MM/yyyy. 2. Current system date + time in dd/MM/yyyy hh:mm:ss. 3. Current zoned date-time including timezone (e.g., IST, PST). Real-life applications - Formatting timestamps in logging frameworks. - Displaying human-readable dates in UIs, reports, or invoices. - Parsing/printing ISO or custom date formats in APIs. - Handling time zones for global applications. Notes - `hh` prints 12-hour format; use `HH` for 24-hour. - `z` gives short timezone name (e.g., IST), `Z` gives numeric offset (e.g., +0530). - Thread-safe: DateTimeFormatter is immutable and safe for concurrent use. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e978e51 commit 314f06a

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

Section26DateandTimeAPI/src/DateandTimeAPI/LocalDateTimeDemo.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@
22
import java.time.*;
33
import java.time.MonthDay;
44

5-
/*
6-
Explanation of Date-Time Classes Mentioned:
7-
8-
ZonedDateTime ? Represents date and time with time zone.
9-
OffsetDateTime ? Date-time with an offset from UTC.
10-
OffsetTime ? Time with an offset from UTC.
11-
ZoneId ? Represents time zone ID.
12-
MonthDay ? Represents a month and day (e.g., birthday).
13-
YearMonth ? Represents a year and month.
14-
Year ? Represents only a year.
15-
Period ? Represents a period (e.g., 2 years, 3 months).
16-
Duration ? Represents a duration (e.g., 5 hours, 30 minutes).
17-
Instant ? Represents a timestamp (date-time in UTC).
18-
*/
19-
205
public class LocalDateTimeDemo {
216
public static void main(String[] args) {
7+
// Current date-time with time zone information (e.g., Asia/Kolkata, UTC offset)
228
ZonedDateTime zonedDateTime = ZonedDateTime.now();
9+
10+
// Current date-time with only UTC offset (e.g., +05:30) but without time zone rules
2311
OffsetDateTime offsetDateTime = OffsetDateTime.now();
12+
13+
// Current time of day with an offset from UTC (time only, no date)
2414
OffsetTime offsetTime = OffsetTime.now();
15+
16+
// The system’s default time zone (e.g., Asia/Kolkata, America/New_York)
2517
ZoneId zoneId = ZoneId.systemDefault();
18+
19+
// Represents a month and day without a year (useful for recurring events like birthdays)
2620
MonthDay monthDay = MonthDay.now();
21+
22+
// Represents a combination of year and month (useful for credit card expiry, payroll, etc.)
2723
YearMonth yearMonth = YearMonth.now();
24+
25+
// Represents only the year (useful for historical data, age calculation, etc.)
2826
Year year = Year.now();
29-
Period period = Period.of(2, 3, 5); // 2 years, 3 months, 5 days
27+
28+
// Represents a date-based amount of time (e.g., 2 years, 3 months, 5 days)
29+
Period period = Period.of(2, 3, 5);
30+
31+
// Represents a time-based amount of time (e.g., 5 hours, 30 seconds)
3032
Duration duration = Duration.ofHours(5);
33+
34+
// A point in time on the UTC timeline (timestamp)
3135
Instant instant = Instant.now();
3236

37+
// Print results
3338
System.out.println("ZonedDateTime: " + zonedDateTime);
3439
System.out.println("OffsetDateTime: " + offsetDateTime);
3540
System.out.println("OffsetTime: " + offsetTime);
@@ -42,3 +47,16 @@ public static void main(String[] args) {
4247
System.out.println("Instant: " + instant);
4348
}
4449
}
50+
51+
/*
52+
1. ZonedDateTime → Full date-time with a time zone (rules + offset).
53+
2. OffsetDateTime → Date-time with only a fixed UTC offset (no daylight saving rules).
54+
3. OffsetTime → Time of day + offset from UTC (no date).
55+
4. ZoneId → Represents a time zone identifier (like "Asia/Kolkata").
56+
5. MonthDay → Stores month + day, without year (e.g., 12-25 → Christmas).
57+
6. YearMonth → Stores year + month (useful for card expiry).
58+
7. Year → Stores only year value.
59+
8. Period → Human-readable period (years, months, days).
60+
9. Duration → Machine-readable time span (hours, minutes, seconds, nanoseconds).
61+
10. Instant → Machine timestamp, precise point on UTC timeline.
62+
*/

0 commit comments

Comments
 (0)