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