-
-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add concept exercise booking-up-for-beauty * Add datetime concept skeleton * Add concept and exercise to config.json * Fix Markdown lint errors * Add copy for datetime concept * Add datetime concept to existing practice exercises * Rename concept to "Date-Time" * Shorten concept introduction a bit
- Loading branch information
1 parent
456356f
commit f179bee
Showing
16 changed files
with
786 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"blurb": "There are several classes in Java to work with dates and time.", | ||
"authors": ["sanderploegsma"], | ||
"contributors": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# About | ||
|
||
The `java.time` package introduced in Java 8 contains several classes to work with dates and time. | ||
|
||
## `LocalDate` | ||
|
||
The [`java.time.LocalDate`][localdate-docs] class represents a date without a time-zone in the [ISO-8601 calendar system][iso-8601], such as `2007-12-03`: | ||
|
||
```java | ||
LocalDate date = LocalDate.of(2007, 12, 3); | ||
``` | ||
|
||
Dates can be compared to other dates: | ||
|
||
```java | ||
LocalDate date1 = LocalDate.of(2007, 12, 3); | ||
LocalDate date2 = LocalDate.of(2007, 12, 4); | ||
|
||
date1.isBefore(date2); | ||
// => true | ||
|
||
date1.isAfter(date2); | ||
// => false | ||
``` | ||
|
||
A `LocalDate` instance has getters to retrieve time portions from it: | ||
|
||
```java | ||
LocalDate date = LocalDate.of(2007, 12, 3); | ||
|
||
date.getYear(); | ||
// => 2007 | ||
|
||
date.getMonthValue(); | ||
// => 12 | ||
|
||
date.getDayOfMonth(); | ||
// => 3 | ||
``` | ||
|
||
A `LocalDate` instance has methods to add time units to it. | ||
|
||
```exercism/note | ||
These methods return a _new_ `LocalDate` instance and do not update the existing instance, as the `LocalDate` class is immutable. | ||
``` | ||
|
||
```java | ||
LocalDate date = LocalDate.of(2007, 12, 3); | ||
|
||
date.addDays(3); | ||
// => 2007-12-06 | ||
|
||
date.addMonths(1); | ||
// => 2008-01-03 | ||
|
||
date.addYears(1); | ||
// => 2008-12-03 | ||
``` | ||
|
||
## `LocalDateTime` | ||
|
||
The [`java.time.LocalDateTime`][localdatetime-docs] class represents a date-time without a time-zone in the [ISO-8601 calendar system][iso-8601], such as `2007-12-03T10:15:30`: | ||
|
||
```java | ||
LocalDateTime datetime = LocalDateTime.of(2007, 12, 3, 10, 15, 30); | ||
|
||
datetime.getYear(); | ||
// => 2007 | ||
|
||
datetime.getMonthValue(); | ||
// => 12 | ||
|
||
datetime.getDayOfMonth(); | ||
// => 3 | ||
|
||
datetime.getHours(); | ||
// => 10 | ||
|
||
datetime.getMinutes(); | ||
// => 15 | ||
|
||
datetime.getSeconds(); | ||
// => 30 | ||
``` | ||
|
||
Like the `LocalDate` class, a `LocalDateTime` instance has the same methods to compare to other `LocalDateTime`s and to add time units to it. | ||
|
||
It is also possible to convert a `LocalDate` instance into a `LocalDateTime`: | ||
|
||
```java | ||
LocalDate date = LocalDate.of(2007, 12, 3); | ||
LocalDateTime datetime = date.atTime(10, 15, 30); | ||
datetime.toString(); | ||
// => "2007-12-03T10:15:30" | ||
``` | ||
|
||
## Formatting datetimes | ||
|
||
Both `LocalDate` and `LocalDateTime` use the [ISO-8601][iso-8601] standard notation when converting from and to a `String`. | ||
|
||
```java | ||
LocalDateTime datetime = LocalDateTime.of(2007, 12, 3, 10, 15, 30); | ||
LocalDateTime parsed = LocalDateTime.parse("2007-12-03T10:15:30"); | ||
|
||
datetime.isEqual(parsed); | ||
// => true | ||
``` | ||
|
||
Attempting to parse a `LocalDate` or `LocalDateTime` from a `String` like this using a different format is not possible. | ||
Instead, to format dates using a custom format, you should use the [`java.time.format.DateTimeFormatter`][datetimeformatter-docs]: | ||
|
||
```java | ||
DateTimeFormatter parser = DateTimeFormatter.ofPattern("dd/MM/yyyy"); | ||
LocalDate date = LocalDate.parse("03/12/2007", formatter); | ||
|
||
DateTimeFormatter printer = DateTimeFormatter.ofPattern("MMMM d, yyyy"); | ||
printer.format(date); | ||
// => "December 3, 2007" | ||
``` | ||
|
||
[iso-8601]: https://en.wikipedia.org/wiki/ISO_8601 | ||
[localdate-docs]: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html | ||
[localdatetime-docs]: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html | ||
[datetimeformatter-docs]: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Introduction | ||
|
||
The `java.time` package introduced in Java 8 contains several classes to work with dates and time. | ||
|
||
## `LocalDate` | ||
|
||
The `java.time.LocalDate` class represents a date without a time-zone in the [ISO-8601 calendar system][iso-8601], such as `2007-12-03`: | ||
|
||
```java | ||
LocalDate date = LocalDate.of(2007, 12, 3); | ||
``` | ||
|
||
Dates can be compared to other dates: | ||
|
||
```java | ||
LocalDate date1 = LocalDate.of(2007, 12, 3); | ||
LocalDate date2 = LocalDate.of(2007, 12, 4); | ||
|
||
date1.isBefore(date2); | ||
// => true | ||
|
||
date1.isAfter(date2); | ||
// => false | ||
``` | ||
|
||
A `LocalDate` instance has getters to retrieve time portions from it: | ||
|
||
```java | ||
LocalDate date = LocalDate.of(2007, 12, 3); | ||
|
||
date.getDayOfMonth(); | ||
// => 3 | ||
``` | ||
|
||
A `LocalDate` instance has methods to add time units to it: | ||
|
||
```exercism/note | ||
These methods return a _new_ `LocalDate` instance and do not update the existing instance, as the `LocalDate` class is immutable. | ||
``` | ||
|
||
```java | ||
LocalDate date = LocalDate.of(2007, 12, 3); | ||
|
||
date.addDays(3); | ||
// => 2007-12-06 | ||
``` | ||
|
||
## `LocalDateTime` | ||
|
||
The `java.time.LocalDateTime` class represents a date-time without a time-zone in the [ISO-8601 calendar system][iso-8601], such as `2007-12-03T10:15:30`: | ||
|
||
```java | ||
LocalDateTime datetime = LocalDateTime.of(2007, 12, 3, 10, 15, 30); | ||
``` | ||
|
||
You can convert a `LocalDate` instance into a `LocalDateTime`: | ||
|
||
```java | ||
LocalDate date = LocalDate.of(2007, 12, 3); | ||
LocalDateTime datetime = date.atTime(10, 15, 30); | ||
datetime.toString(); | ||
// => "2007-12-03T10:15:30" | ||
``` | ||
|
||
## Formatting datetimes | ||
|
||
Both `LocalDate` and `LocalDateTime` use the [ISO-8601][iso-8601] standard notation when converting from and to a `String`. | ||
|
||
```java | ||
LocalDateTime datetime = LocalDateTime.of(2007, 12, 3, 10, 15, 30); | ||
LocalDateTime parsed = LocalDateTime.parse("2007-12-03T10:15:30"); | ||
|
||
datetime.isEqual(parsed); | ||
// => true | ||
``` | ||
|
||
Attempting to parse a `LocalDate` or `LocalDateTime` from a `String` like this using a different format is not possible. | ||
Instead, to format dates using a custom format, you should use the `java.time.format.DateTimeFormatter`: | ||
|
||
```java | ||
DateTimeFormatter parser = DateTimeFormatter.ofPattern("dd/MM/yyyy"); | ||
LocalDate date = LocalDate.parse("03/12/2007", formatter); | ||
|
||
DateTimeFormatter printer = DateTimeFormatter.ofPattern("MMMM d, yyyy"); | ||
printer.format(date); | ||
// => "December 3, 2007" | ||
``` | ||
|
||
[iso-8601]: https://en.wikipedia.org/wiki/ISO_8601 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[ | ||
{ | ||
"url": "https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html", | ||
"description": "LocalDate documentation" | ||
}, | ||
{ | ||
"url": "https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html", | ||
"description": "LocalDateTime documentation" | ||
}, | ||
{ | ||
"url": "https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html", | ||
"description": "DateTimeFormatter documentation" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Hints | ||
|
||
## 1. Parse appointment date | ||
|
||
- The `LocalDateTime` class has a method to [parse][localdatetime-parse] a `String` to a `LocalDateTime`. | ||
- Use a [`DateTimeFormatter`][datetimeformatter-docs] with a custom pattern to parse a non-standard date/time format. | ||
|
||
## 2. Check if an appointment has already passed | ||
|
||
- `LocalDateTime` instances have [methods][localdatetime-methods] to compare them to other `LocalDateTime`s. | ||
- There is a [method][localdatetime-methods] to retrieve the current date and time. | ||
|
||
## 3. Check if appointment is in the afternoon | ||
|
||
- Accessing the time portion of a `LocalDateTime` instance can de done through one of its [methods][localdatetime-methods]. | ||
|
||
## 4. Describe the time and date of the appointment | ||
|
||
- Use a [`DateTimeFormatter`][datetimeformatter-docs] with a custom pattern to format the `LocalDateTime`. | ||
- The tests are running as if running on a machine in the United States, so make sure that the formatter [uses the correct locale][datetimeformatter-ofpattern-with-locale]. | ||
|
||
## 5. Return the anniversary date | ||
|
||
- The `LocalDate` class has a [method][localdate-methods] to retrieve the current date. | ||
- Accessing the year of a `LocalDate` instance can de done through one of its [methods][localdate-methods]. | ||
|
||
[localdate-methods]: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html | ||
[localdatetime-methods]: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html | ||
[localdatetime-parse]: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#parse-java.lang.CharSequence-java.time.format.DateTimeFormatter- | ||
[datetimeformatter-docs]: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html | ||
[datetimeformatter-ofpattern-with-locale]: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ofPattern-java.lang.String-java.util.Locale- |
53 changes: 53 additions & 0 deletions
53
exercises/concept/booking-up-for-beauty/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Instructions | ||
|
||
In this exercise you'll be working on an appointment scheduler for a beauty salon in New York that opened on September 15th in 2012. | ||
|
||
## 1. Parse appointment date | ||
|
||
Implement the `AppointmentScheduler.schedule()` method to parse a textual representation of an appointment date into the corresponding `LocalDateTime`: | ||
|
||
```java | ||
AppointmentScheduler scheduler = new AppointmentScheduler(); | ||
scheduler.schedule("7/25/2019 13:45:00"); | ||
// => LocalDateTime.of(2019, 7, 25, 13, 45, 0) | ||
``` | ||
|
||
## 2. Check if an appointment has already passed | ||
|
||
Implement the `AppointmentScheduler.hasPassed()` method that takes an appointment date and checks if the appointment was somewhere in the past: | ||
|
||
```java | ||
AppointmentScheduler scheduler = new AppointmentScheduler(); | ||
scheduler.hasPassed(LocalDateTime.of(1999, 12, 31, 9, 0, 0)); | ||
// => true | ||
``` | ||
|
||
## 3. Check if appointment is in the afternoon | ||
|
||
Implement the `AppointmentScheduler.isAfternoonAppointment()` method that takes an appointment date and checks if the appointment is in the afternoon (>= 12:00 and < 18:00): | ||
|
||
```java | ||
AppointmentScheduler scheduler = new AppointmentScheduler(); | ||
scheduler.isAfternoonAppointment(LocalDateTime.of(2019, 03, 29, 15, 0, 0)) | ||
// => true | ||
``` | ||
|
||
## 4. Describe the time and date of the appointment | ||
|
||
Implement the `AppointmentScheduler.getDescription()` method that takes an appointment date and returns a description of that date and time: | ||
|
||
```java | ||
AppointmentScheduler scheduler = new AppointmentScheduler(); | ||
scheduler.getDescription(LocalDateTime.of(2019, 03, 29, 15, 0, 0)) | ||
// => "You have an appointment on Friday, March 29, 2019, at 3:00 PM." | ||
``` | ||
|
||
## 5. Return the anniversary date | ||
|
||
Implement the `AppointmentScheduler.getAnniversaryDate()` method that returns this year's anniversary date, which is September 15th: | ||
|
||
```java | ||
AppointmentScheduler scheduler = new AppointmentScheduler(); | ||
scheduler.getAnniversaryDate() | ||
// => LocalDate.of(2023, 9, 15) | ||
``` |
Oops, something went wrong.