diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b0e4882 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +*.class +*.log + +# sbt specific +.cache +.history +.lib +dist/* +target/ +lib_managed/ +src_managed/ +project/boot/ +project/plugins/project/ + +# Scala-IDE specific +.scala_dependencies +.worksheet +.idea diff --git a/scala_cli_app/build.sbt b/scala_cli_app/build.sbt new file mode 100644 index 0000000..a18c7c9 --- /dev/null +++ b/scala_cli_app/build.sbt @@ -0,0 +1,7 @@ +name := "scala_cli_app" + +version := "1.0" + +scalaVersion := "2.12.10" + +libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test" diff --git a/scala_cli_app/project/build.properties b/scala_cli_app/project/build.properties new file mode 100644 index 0000000..6624da7 --- /dev/null +++ b/scala_cli_app/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.3.5 diff --git a/scala_cli_app/src/main/scala/app/CLIApp.scala b/scala_cli_app/src/main/scala/app/CLIApp.scala new file mode 100644 index 0000000..099f395 --- /dev/null +++ b/scala_cli_app/src/main/scala/app/CLIApp.scala @@ -0,0 +1,26 @@ +package app + +import util.DateUtil +import scala.annotation.tailrec +import scala.io.StdIn.readLine +import scala.util.{Success, Try} + +object CLIApp extends App { + val date1 = DateUtil.str2Date(getInput) + val date2 = DateUtil.str2Date(getInput) + println(s"date1: ${DateUtil.date2Str(date1)}") + println(s"date2: ${DateUtil.date2Str(date2)}") + println(s"yearsBetween: ${DateUtil.yearsBetween(date1, date2)}") + println(s"compare : ${DateUtil.compare(date1, date2)}") + + @tailrec + def getInput: String = { + println("入力してください (例: 2020-01-01)") + val input = readLine() + + Try(DateUtil.str2Date(input)) match { + case Success(_) => input + case _ => getInput + } + } +} diff --git a/scala_cli_app/src/main/scala/util/DateUtil.scala b/scala_cli_app/src/main/scala/util/DateUtil.scala new file mode 100644 index 0000000..f4f79cf --- /dev/null +++ b/scala_cli_app/src/main/scala/util/DateUtil.scala @@ -0,0 +1,39 @@ +package util + +import java.time.{LocalDate, LocalDateTime} +import java.time.format.{DateTimeFormatter, ResolverStyle} +import java.time.temporal.ChronoUnit.YEARS + +object DateUtil { + private val dateFormatter = + DateTimeFormatter.ofPattern("uuuu-MM-dd") + .withResolverStyle(ResolverStyle.STRICT) + + private val dateTimeFormatter = + DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm") + .withResolverStyle(ResolverStyle.STRICT) + + def str2Date(str: String): LocalDate = { + LocalDate.parse(str, dateFormatter) + } + + def str2DateTime(str: String): LocalDateTime = { + LocalDateTime.parse(str, dateTimeFormatter) + } + + def date2Str(date: LocalDate): String = { + date.format(dateFormatter) + } + + def dateTime2Str(date: LocalDateTime): String = { + date.format(dateTimeFormatter) + } + + def yearsBetween(localDate1: LocalDate, localDate2: LocalDate): Int = { + YEARS.between(localDate1, localDate2).toInt + } + + def compare(localDate1: LocalDate, localDate2: LocalDate): Int = { + localDate1.compareTo(localDate2).abs + } +} diff --git a/scala_cli_app/src/test/scala/util/DateUtilSpec.scala b/scala_cli_app/src/test/scala/util/DateUtilSpec.scala new file mode 100644 index 0000000..4fe13ab --- /dev/null +++ b/scala_cli_app/src/test/scala/util/DateUtilSpec.scala @@ -0,0 +1,50 @@ +package util + +import java.time.{LocalDate, LocalDateTime} +import org.scalatest.{FunSpec, Matchers} + +class DateUtilSpec extends FunSpec with Matchers{ + describe("str2Date") { + it("String => LocalDate") { + val expected = LocalDate.of(2020, 1, 1) + DateUtil.str2Date("2020-01-01") shouldEqual expected + } + } + + describe("str2DateTime") { + it("String => LocalDateTime") { + val expected = LocalDateTime.of(2020, 1, 1, 0, 30, 0) + DateUtil.str2DateTime("2020-01-01 00:30") shouldEqual expected + } + } + + describe("date2Str") { + it("LocalDate => String") { + val input = LocalDate.of(2020, 1, 1) + DateUtil.date2Str(input) shouldEqual "2020-01-01" + } + } + + describe("dateTime2Str") { + it("LocalDate => String") { + val input = LocalDateTime.of(2020, 1, 1, 0, 30, 0) + DateUtil.dateTime2Str(input) shouldEqual "2020-01-01 00:30" + } + } + + describe("yearsBetween") { + it("calculate age") { + val input1 = LocalDate.of(2018, 4, 1) + val input2 = LocalDate.of(2020, 1, 1) + DateUtil.yearsBetween(input1, input2) shouldEqual 1 + } + } + + describe("compare") { + it("calculate age") { + val input1 = LocalDate.of(2018, 4, 1) + val input2 = LocalDate.of(2020, 1, 1) + DateUtil.compare(input1, input2) shouldEqual 2 + } + } +}