Skip to content

Commit 8db7baf

Browse files
author
Anass Rach
committed
Add datetime tricky card!
1 parent 763a0e7 commit 8db7baf

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

datetime.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,93 @@ dates.stream()
254254

255255
---
256256

257+
## 🃏 Daylight Saving Time "Fall Back" Calculations
258+
259+
**Rule:** When DST ends ("fall back"), the **same local time occurs twice**, creating a 25-hour day where time calculations must account for the repeated hour.
260+
261+
**Timeline Visualization for November 5, 2023 DST End in America/Chicago:**
262+
263+
```
264+
Regular Timeline: DST "Fall Back" Timeline:
265+
12:00 AM ──────────────► 12:00 AM ──────────────►
266+
01:00 AM ──────────────► 01:00 AM ──────────────►
267+
02:00 AM ──────────────► 02:00 AM (first occurrence) ──┐
268+
03:00 AM ──────────────► │ Clock jumps back
269+
04:00 AM ──────────────► 01:00 AM (after fall back) ──┘
270+
02:00 AM (second occurrence) ──►
271+
03:00 AM ──────────────►
272+
```
273+
274+
**The Problem:** When calculating time between 3:00 AM and 1:00 AM on DST end day:
275+
276+
```java
277+
import java.time.*;
278+
import java.time.temporal.ChronoUnit;
279+
280+
// DST ends November 5, 2023 at 2:00 AM in America/Chicago
281+
// Clock "falls back" from 2:00 AM to 1:00 AM
282+
283+
LocalDateTime morning = LocalDateTime.of(2023, Month.NOVEMBER, 5, 3, 0); // 3:00 AM
284+
ZonedDateTime chicagoMorning = ZonedDateTime.of(morning, ZoneId.of("America/Chicago"));
285+
286+
LocalDateTime early = LocalDateTime.of(2023, Month.NOVEMBER, 5, 1, 0); // 1:00 AM
287+
ZonedDateTime chicagoEarly = ZonedDateTime.of(early, ZoneId.of("America/Chicago"));
288+
289+
long hoursBetween = ChronoUnit.HOURS.between(chicagoMorning, chicagoEarly);
290+
System.out.println("Hours from 3:00 AM to 1:00 AM: " + hoursBetween); // -2
291+
```
292+
293+
**Why the result is -2:**
294+
295+
**Step-by-step timeline:**
296+
1. **3:00 AM** (starting point)
297+
2. Go back 1 hour → **2:00 AM** (second occurrence, after fall back)
298+
3. Go back 1 more hour → **1:00 AM** (after the repeated hour)
299+
300+
**Total:** 2 hours backward = **-2**
301+
302+
**The Key Insight:**
303+
- From 3:00 AM to 1:00 AM normally would be -2 hours
304+
- On DST "fall back" day, there's an extra hour (1:00-2:00 AM occurs twice)
305+
- ZonedDateTime automatically picks the **later occurrence** (after fall back)
306+
- So we still need -2 hours to go from 3:00 AM to the 1:00 AM that comes after the time change
307+
308+
**Extended example with different times:**
309+
```java
310+
// DST ends March 26, 2023 at 3:00 AM in Europe/Berlin (falls back to 2:00 AM)
311+
LocalDateTime breakfast = LocalDateTime.of(2023, Month.MARCH, 26, 4, 30); // 4:30 AM
312+
ZonedDateTime berlinBreakfast = ZonedDateTime.of(breakfast, ZoneId.of("Europe/Berlin"));
313+
314+
LocalDateTime midnight = LocalDateTime.of(2023, Month.MARCH, 26, 2, 15); // 2:15 AM
315+
ZonedDateTime berlinMidnight = ZonedDateTime.of(midnight, ZoneId.of("Europe/Berlin"));
316+
317+
long minutesDiff = ChronoUnit.MINUTES.between(berlinBreakfast, berlinMidnight);
318+
System.out.println("Minutes from 4:30 AM to 2:15 AM: " + minutesDiff); // -195
319+
320+
// Breakdown: 4:30 AM → 2:15 AM = -2 hours 15 minutes = -135 minutes
321+
// But DST adds an extra hour, so -135 - 60 = -195 minutes
322+
```
323+
324+
**Visual Timeline for the Calculation:**
325+
```
326+
DST End Day (25-hour day):
327+
├─ 12:00 AM
328+
├─ 01:00 AM
329+
├─ 02:00 AM (first time) ──┐
330+
├─ 02:00 AM (repeated) ──┘ ← Extra hour here!
331+
├─ 02:15 AM (our target) ────────┐
332+
├─ 03:00 AM │
333+
├─ 04:00 AM │ 195 minutes difference
334+
└─ 04:30 AM (our start) ────────┘
335+
```
336+
337+
**💡 Learning Tip:** Remember "FALL BACK = EXTRA HOUR" - When DST ends, one hour is repeated (1:00-2:00 AM happens twice), making time calculations longer than expected.
338+
339+
**Q:** On DST end day, if you calculate hours between 4:00 AM and 2:00 AM, why might the result be -3 instead of -2?
340+
**A:** Because the 2:00 AM hour occurs twice on DST end day. Going from 4:00 AM to 2:00 AM crosses the repeated hour, requiring an extra hour: 4→3→2(first)→2(repeated) = 3 hours backward = -3.
341+
342+
---
343+
257344
## 🃏 Localization - Locale and Resource Bundles
258345

259346
**Rule:** Localization uses **Locale** for region/language and **ResourceBundle** for externalized text.

0 commit comments

Comments
 (0)