@@ -3,8 +3,9 @@ package com.baeldung.scala.date
33import org .scalatest .matchers .should .Matchers
44import org .scalatest .wordspec .AnyWordSpec
55
6+ import java .time .format .DateTimeFormatter
67import java .time .temporal .ChronoField
7- import java .time .{ZoneId , ZonedDateTime }
8+ import java .time .{LocalDate , LocalDateTime , ZoneId , ZonedDateTime }
89import java .util .Calendar
910import scala .util .Try
1011
@@ -73,5 +74,21 @@ class DateParserUnitTest extends AnyWordSpec with Matchers {
7374 attemptedParse.failed.get.getMessage.contains(" could not be parsed" )
7475 )
7576 }
77+
78+ " parse ISO date using java.time" in {
79+ val dateStr = " 2024-09-19"
80+ LocalDate .parse(dateStr) shouldBe LocalDate .of(2024 , 9 , 19 )
81+ }
82+ " parse non ISO date string" in {
83+ val dateStr = " 19.09.2024"
84+ val formatter = DateTimeFormatter .ofPattern(" dd.MM.yyyy" )
85+ LocalDate .parse(dateStr, formatter) shouldBe LocalDate .of(2024 , 9 , 19 )
86+ }
87+ " parse datetime using java.time" in {
88+ val dateStr = " 19.09.2024 10:20:30"
89+ val formatter = DateTimeFormatter .ofPattern(" dd.MM.yyyy HH:mm:ss" )
90+ LocalDateTime .parse(dateStr, formatter) shouldBe LocalDateTime .of(2024 , 9 ,
91+ 19 , 10 , 20 , 30 )
92+ }
7693 }
7794}
0 commit comments