AlienHead
AlienHead

Reputation: 125

Date/Time formatting Scala

I'm trying to assert date and time displayed on the page

Problem is it's returning value of "2017-03-11T09:00" instead of "2017-03-11 09:00:00" and I'm confused why as the pattern = yyyy-MM-dd HH:mm:ss

Any ideas?

def getDate :String = {
val timeStamp = find(xpath("//*[@id=\"content\"]/article/div/div/table/tbody/tr[5]/td/div/p[4]")).get.underlying.getText
val stripDate: Array[String] = timeStamp.split("Timestamp:\n")
stripDate(1)
}


def datePattern(date: String): LocalDateTime = {
val pattern: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
val result = LocalDateTime.parse(date, pattern)
result
}

def checkDatePattern() = datePattern(getDate).toString shouldBe getDate

Upvotes: 0

Views: 4665

Answers (2)

AlienHead
AlienHead

Reputation: 125

I've managed to get the result I wanted by just deleting some parts of the code. As long as the date is in displayed in the correct format, the test passes if it's displayed in an incorrect format it fails, which is good enough for me. Thanks for your input. CASE CLOSED

 def getDate :String = {
val timeStamp = find(xpath("//*[@id=\"content\"]/article/div/div/table/tbody/tr[5]/td/div/p[4]")).get.underlying.getText
val stripDate: Array[String] = timeStamp.split("Timestamp:\n")
stripDate(1)
 }


def datePattern(date: String): LocalDateTime = {
val pattern: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
LocalDateTime.parse(date, pattern)
}

def checkDatePattern() = datePattern(getDate)

Upvotes: 1

Jasper-M
Jasper-M

Reputation: 15086

The DateTimeFormatter only gets used for the parse operation. It doesn't influence the result of toString. If you want to convert your LocalDateTime to a String in a certain format you have to call

date.format(pattern)

Upvotes: 3

Related Questions