13 What time is it?

 

In this capstone, you will

Create a package for your application

Import and use the java.time package

Code using classes, objects, and traits together with access modifiers

Define an executable object

Time zones can be challenging to deal with, particularly in distributed teams and collaborations. In this capstone, you’ll implement an executable application that uses sbt to print the current time in a given time zone.

13.1 What time is it?

The goal of this capstone is to create a small program using sbt: it should start, ask the user to enter a time zone, use the input to compute the current time in it, and display the result in the terminal in a human-readable format, such as RFC 1123 (e.g., “Fri, 27 Apr 2018 11:44:35 +0200”). Let’s keep things simple and let the script crash if the user enters an invalid time zone; you’ll learn how to handle exceptions in the next unit.

13.1.1 sbt project setup

First, you need to set up the sbt project. There are different ways of achieving this: you can create an sbt project using your IDE or apply the hello-world Giter8 template by typing the command sbt new scala/hello-world.g8. When using the hello-world Giter8 template do not forget to delete the Main.scala file in the src/main/scala folder. Alternatively, you can also create an empty sbt project manually as follows:

  1. In your project directory, create a build.sbt file containing your Scala version, the name, and your project version. Listing 13.1 An example of build.sbt file

13.1.2 The business logic layer

13.1.3 The TimeApp executable object

13.1.4 Let’s try it out!

13.2 Possible improvements to our solution

Summary