Repository navigation
Conversation
Introduces the `durations-and-periods` concept, which covers the date-based `Period`, the time-based `Duration` and counting units with `ChronoUnit.between()`, including common pitfalls such as `Period.getDays()` not being a total and month-end clamping. The new `gym-front-desk` concept exercise teaches it through a climbing gym front desk: membership end dates and lengths (`Period`), days left on a membership (`ChronoUnit.DAYS`) and the length of a visit printed on a receipt (`Duration`, `toHours()` vs `toMinutesPart()`). Also adds the concept to the `practices` and `prerequisites` of the `gigasecond` practice exercise. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| | | `Period` | `Duration` | | ||
| | --- | --- | --- | | ||
| | Stores | years, months and days | seconds and nanoseconds | | ||
| | Used with | `LocalDate` | `LocalTime`, `LocalDateTime`, `Instant` | | ||
| | Example | "the membership lasts 1 month" | "the visit lasted 95 minutes" | | ||
| | `toString()` | `P1Y2M15D` | `PT1H35M` | |
There was a problem hiding this comment.
A couple of comments on this table:
- I think
Periodcan also be used with bothLocalDateTimeandInstant, not justLocalDate. - I think the "Example" and "toString()" rows are a bit confusing because the "Example" doesn't show how the period is created for the "toString()". Is the first "toString" supposed to be "P1M"? In any case, you already show some examples later, so it might not be needed in this table.
There was a problem hiding this comment.
Thanks, good catch! Period does work with LocalDateTime as well, so I've added it to the table and added an example to the "Adding a period to a date" section.
With Instant it only partly works: a period of days can be added, but Instant.plus(Period.ofMonths(1)) throws UnsupportedTemporalTypeException, because an Instant has no calendar. I've explained that in the text below the LocalDateTime example rather than in the table.
You're right about the "Example" and "toString()" rows too: they didn't match each other, and the sections below already cover both, so I've removed them.
Fixed in 7d1b8ab.
| The `java.time` package also has two classes that describe an _amount_ of time: | ||
|
|
||
| - `java.time.Period` is an amount of time in years, months and days, such as "2 months and 5 days". | ||
| - `java.time.Duration` is an amount of time in seconds and nanoseconds, such as "1 hour and 30 minutes". |
There was a problem hiding this comment.
Do we need to mention the other time units?
| - `java.time.Duration` is an amount of time in seconds and nanoseconds, such as "1 hour and 30 minutes". | |
| - `java.time.Duration` is an amount of time in hours, minutes, seconds, milliseconds and nanoseconds, such as "1 hour and 30 minutes". |
There was a problem hiding this comment.
Agreed, I've applied the suggestion in 7d1b8ab and used the same wording in about.md and in the exercise hints.
I've kept the "Stores: seconds and nanoseconds" row in the about.md table, since it explains why, for example, Duration.ofDays(2) prints as PT48H.
|
@rabestro Reminder to have a look here when you got a chance |
- Describe Duration in hours, minutes, seconds, milliseconds and nanoseconds, as suggested in review, in the concept docs and hints - Note that a Period can be used with LocalDateTime too, and that an Instant only accepts a Period of days - Drop the confusing "Example" and "toString()" rows from the table - Regenerate the exercise introduction from the updated concept Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pull request
Description
This PR adds a new concept,
durations-and-periods, and a concept exercise that teaches it,gym-front-desk.The idea was discussed and approved on the forum: Add a concept for Duration and Period.
Motivation
The
datetimeconcept covers points in time (LocalDate,LocalDateTime) but not amounts of time, andbeauty-salon-regularsexplicitly leavesDurationandPeriodout of scope.Students who do date arithmetic in practice exercises either do it by hand or run into the usual pitfalls, the most common being
Period.between(a, b).getDays()used as a total number of days.Concept:
durations-and-periodsintroduction.md:Period(factory methods,plus(), month-end clamping,Period.between()and its parts), counting units withChronoUnit.X.between(), andDuration(factory methods,Duration.between(),toXxx()vstoXxxPart()).about.md: adds ISO-8601 parsing,until(),toTotalMonths(),normalized()and unit-by-unitequals(),UnsupportedTemporalTypeExceptionwhen mixing dates and durations, comparing and arithmetic with durations, and a short aside on whyPeriod.ofDays(1)andDuration.ofDays(1)differ across a DST change.jshell.Exercise:
gym-front-deskA climbing gym front desk, with five tasks:
membershipEnd(): add aPeriodof months to a start date (tests include month-end clamping and leap years).membershipLength():Period.between().daysLeft(): total days withChronoUnit.DAYS.between()(a test spans several months, soPeriod.getDays()fails it).visitDuration():Duration.between()for a visit, including one past midnight.formatVisit():"1h 35m"on a receipt, usingtoHours()andtoMinutesPart()(a test with a visit over 24 hours catchestoHoursPart()).Prerequisites:
datetime,strings.design.mdlists suggested analyzer rules.Config changes
config.json(UUIDs16500ebe-104e-44c3-84b3-3126a49bdb89and6553f255-154f-441d-a6e1-a428434f9653) and the exercise toexercises/settings.gradle.durations-and-periodsto thepracticesandprerequisitesofgigasecond, which adds a gigasecond to a date-time.Happy to drop it from
prerequisitesif you'd rather not lock the exercise behind the new concept.Checks run locally
./gradlew test,checkandcompileStarterTestJavafor the exercise (JDK 25): 23 tests pass, no Checkstyle issues.configlet lintandconfiglet generate(the exercise'sintroduction.mdis generated from the concept).markdownlint-cli2on the new Markdown files.The
i18n / completenesscheck will need theready-to-translatelabel to queue the new English for translation.Reviewer Resources:
Track Policies
🤖 Generated with Claude Code