Tracks
/
Java
Java
/
Exercises
/
Booking Up For Beauty
Booking Up For Beauty

Booking Up For Beauty

Learning Exercise

Introduction

Date-Time

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, such as 2007-12-03:

LocalDate date = LocalDate.of(2007, 12, 3);

Dates can be compared to other dates:

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:

LocalDate date = LocalDate.of(2007, 12, 3);

date.getDayOfMonth();
// => 3

A LocalDate instance has methods to add time units to it:

Note

These methods return a new LocalDate instance and do not update the existing instance, as the LocalDate class is immutable.

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, such as 2007-12-03T10:15:30:

LocalDateTime datetime = LocalDateTime.of(2007, 12, 3, 10, 15, 30);

You can convert a LocalDate instance into a LocalDateTime:

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 standard notation when converting from and to a String.

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 parser = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date = LocalDate.parse("03/12/2007", parser);

DateTimeFormatter printer = DateTimeFormatter.ofPattern("MMMM d, yyyy");
printer.format(date);
// => "December 3, 2007"

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:

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:

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):

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:

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:

AppointmentScheduler scheduler = new AppointmentScheduler();
scheduler.getAnniversaryDate()
// => LocalDate.of(<current year>, 9, 15)
Edit via GitHub The link opens in a new window or tab
Java Exercism

Ready to start Booking Up For Beauty?

Sign up to Exercism to learn and master Java with 25 concepts, 148 exercises, and real human mentoring, all for free.