MODULE 1 · TOPIC 9 · VISUAL PROGRAMMING LAB

New Date/Time API

Use immutable java.time objects to calculate dates, durations and zone conversions.

Browser-based simulations: change the inputs, trace the Java statements and inspect results. No arbitrary Java execution, real database, threads or file changes occur here.

1. LocalDate — add days across month boundaries

Observe calendar arithmetic including leap years.

Java example
Program / simulation outputConsole
Memory / data inspectorCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

LocalDate has no time or zone. plusDays returns a new LocalDate and handles calendar boundaries.

Try and observe

Try February 28 in 2027 and 2028. Try February 30.

2. Duration versus Period

Compare elapsed minutes with calendar days.

Java example
Program / simulation outputConsole
Live execution stateCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

Period is date-based. Duration is time-based; Duration.ofDays always counts 24-hour units. Zoned calendar days can differ around DST.

Try and observe

Set days=1 and hours=0; compare the units.

3. Instant and time zones

Display one instant in UTC, India or Japan.

Java example
Program / simulation outputConsole
Live execution stateCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

Instant represents a point on the timeline. ZonedDateTime combines local time with zone rules. The example uses fixed date and three zones without DST on that date.

Try and observe

Set UTC 22:00 and choose India. Watch the date roll over.

4. Hands-on practice and assessment

Programming challenge

Calculate a due date 30 days after a user-selected LocalDate; format it using DateTimeFormatter.ofPattern("dd-MM-yyyy").

Edit the scratchpad below. Copy your code into an actual Java project or IDE to compile and test it.

Quick assessment

Key points and real Java notes

Use ISO-8601 parsing, DateTimeFormatter, and explicit ZoneId for predictable results. Avoid legacy mutable Date/Calendar for new code where java.time is suitable.