Skip to content

Commit

Permalink
Add concept datetime and exercise booking-up-for-beauty (#2590)
Browse files Browse the repository at this point in the history
* 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
sanderploegsma authored Nov 30, 2023
1 parent 456356f commit f179bee
Show file tree
Hide file tree
Showing 16 changed files with 786 additions and 3 deletions.
5 changes: 5 additions & 0 deletions concepts/datetime/.meta/config.json
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": []
}
124 changes: 124 additions & 0 deletions concepts/datetime/about.md
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
89 changes: 89 additions & 0 deletions concepts/datetime/introduction.md
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
14 changes: 14 additions & 0 deletions concepts/datetime/links.json
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"
}
]
27 changes: 24 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@
"numbers",
"strings"
]
},
{
"slug": "booking-up-for-beauty",
"name": "Booking Up For Beauty",
"uuid": "6a514e9a-ed92-4e01-8af7-579d659415a4",
"concepts": [
"datetime"
],
"prerequisites": [
"numbers",
"strings"
]
}
],
"practice": [
Expand Down Expand Up @@ -303,9 +315,10 @@
"name": "Gigasecond",
"uuid": "bf1641c8-dc0d-4d38-9cfe-b4c132ea3553",
"practices": [
"numbers"
"datetime"
],
"prerequisites": [
"datetime",
"numbers"
],
"difficulty": 3
Expand Down Expand Up @@ -885,10 +898,11 @@
"name": "Meetup",
"uuid": "602511d5-7e89-4def-b072-4dd311816810",
"practices": [
"enums",
"for-loops"
"datetime",
"enums"
],
"prerequisites": [
"datetime",
"enums"
],
"difficulty": 7
Expand Down Expand Up @@ -1995,10 +2009,12 @@
"name": "Ledger",
"uuid": "6597548e-176d-49c6-be33-789f4c43867a",
"practices": [
"datetime",
"strings"
],
"prerequisites": [
"classes",
"datetime",
"strings"
],
"difficulty": 5
Expand Down Expand Up @@ -2101,6 +2117,11 @@
"slug": "constructors",
"name": "Constructors"
},
{
"uuid": "16e1b053-f99d-410e-8b3c-054e764da953",
"slug": "datetime",
"name": "Date-Time"
},
{
"uuid": "cb753863-b7c1-4ff6-adeb-ae9a1f39deca",
"slug": "enums",
Expand Down
31 changes: 31 additions & 0 deletions exercises/concept/booking-up-for-beauty/.docs/hints.md
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 exercises/concept/booking-up-for-beauty/.docs/instructions.md
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)
```
Loading

0 comments on commit f179bee

Please sign in to comment.