Skip to content

Commit 1b8cd16

Browse files
authored
Merge pull request #2 from suaaa7/1_cli_app
Create CLI App
2 parents 0579e59 + d25ec70 commit 1b8cd16

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed

.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
*.class
2+
*.log
3+
4+
# sbt specific
5+
.cache
6+
.history
7+
.lib
8+
dist/*
9+
target/
10+
lib_managed/
11+
src_managed/
12+
project/boot/
13+
project/plugins/project/
14+
15+
# Scala-IDE specific
16+
.scala_dependencies
17+
.worksheet
18+
.idea

scala_cli_app/build.sbt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name := "scala_cli_app"
2+
3+
version := "1.0"
4+
5+
scalaVersion := "2.12.10"
6+
7+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test"
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.3.5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package app
2+
3+
import util.DateUtil
4+
import scala.annotation.tailrec
5+
import scala.io.StdIn.readLine
6+
import scala.util.{Success, Try}
7+
8+
object CLIApp extends App {
9+
val date1 = DateUtil.str2Date(getInput)
10+
val date2 = DateUtil.str2Date(getInput)
11+
println(s"date1: ${DateUtil.date2Str(date1)}")
12+
println(s"date2: ${DateUtil.date2Str(date2)}")
13+
println(s"yearsBetween: ${DateUtil.yearsBetween(date1, date2)}")
14+
println(s"compare : ${DateUtil.compare(date1, date2)}")
15+
16+
@tailrec
17+
def getInput: String = {
18+
println("入力してください (例: 2020-01-01)")
19+
val input = readLine()
20+
21+
Try(DateUtil.str2Date(input)) match {
22+
case Success(_) => input
23+
case _ => getInput
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package util
2+
3+
import java.time.{LocalDate, LocalDateTime}
4+
import java.time.format.{DateTimeFormatter, ResolverStyle}
5+
import java.time.temporal.ChronoUnit.YEARS
6+
7+
object DateUtil {
8+
private val dateFormatter =
9+
DateTimeFormatter.ofPattern("uuuu-MM-dd")
10+
.withResolverStyle(ResolverStyle.STRICT)
11+
12+
private val dateTimeFormatter =
13+
DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm")
14+
.withResolverStyle(ResolverStyle.STRICT)
15+
16+
def str2Date(str: String): LocalDate = {
17+
LocalDate.parse(str, dateFormatter)
18+
}
19+
20+
def str2DateTime(str: String): LocalDateTime = {
21+
LocalDateTime.parse(str, dateTimeFormatter)
22+
}
23+
24+
def date2Str(date: LocalDate): String = {
25+
date.format(dateFormatter)
26+
}
27+
28+
def dateTime2Str(date: LocalDateTime): String = {
29+
date.format(dateTimeFormatter)
30+
}
31+
32+
def yearsBetween(localDate1: LocalDate, localDate2: LocalDate): Int = {
33+
YEARS.between(localDate1, localDate2).toInt
34+
}
35+
36+
def compare(localDate1: LocalDate, localDate2: LocalDate): Int = {
37+
localDate1.compareTo(localDate2).abs
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package util
2+
3+
import java.time.{LocalDate, LocalDateTime}
4+
import org.scalatest.{FunSpec, Matchers}
5+
6+
class DateUtilSpec extends FunSpec with Matchers{
7+
describe("str2Date") {
8+
it("String => LocalDate") {
9+
val expected = LocalDate.of(2020, 1, 1)
10+
DateUtil.str2Date("2020-01-01") shouldEqual expected
11+
}
12+
}
13+
14+
describe("str2DateTime") {
15+
it("String => LocalDateTime") {
16+
val expected = LocalDateTime.of(2020, 1, 1, 0, 30, 0)
17+
DateUtil.str2DateTime("2020-01-01 00:30") shouldEqual expected
18+
}
19+
}
20+
21+
describe("date2Str") {
22+
it("LocalDate => String") {
23+
val input = LocalDate.of(2020, 1, 1)
24+
DateUtil.date2Str(input) shouldEqual "2020-01-01"
25+
}
26+
}
27+
28+
describe("dateTime2Str") {
29+
it("LocalDate => String") {
30+
val input = LocalDateTime.of(2020, 1, 1, 0, 30, 0)
31+
DateUtil.dateTime2Str(input) shouldEqual "2020-01-01 00:30"
32+
}
33+
}
34+
35+
describe("yearsBetween") {
36+
it("calculate age") {
37+
val input1 = LocalDate.of(2018, 4, 1)
38+
val input2 = LocalDate.of(2020, 1, 1)
39+
DateUtil.yearsBetween(input1, input2) shouldEqual 1
40+
}
41+
}
42+
43+
describe("compare") {
44+
it("calculate age") {
45+
val input1 = LocalDate.of(2018, 4, 1)
46+
val input2 = LocalDate.of(2020, 1, 1)
47+
DateUtil.compare(input1, input2) shouldEqual 2
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)