[*] Konvertieren einer Zeitstempelzeichenfolge in Millisekunden aus Epoche < /li>
Mit Scala Sprache mit Java -Bibliotheken
[*] Unit -Test
Code [/b]
/> Funktion (innerhalb von Utils -Klasse)
Code: Select all
def stringToMillis(inputString: String): Long = {
// input date string and its formatter
val formatter = DateTimeFormatter.ofPattern("M/d/yyyy h:mm:ss a")
val localTime = LocalDateTime.parse(inputString, formatter)
val zonedTime = localTime.atZone(ZoneOffset.UTC)
zonedTime.toInstant.toEpochMilli
Code: Select all
val currentTime = Instant.now
val expected = currentTime.toEpochMilli
val formatter =
DateTimeFormatter.ofPattern("M/d/yyyy h:mm:ss a").withZone(ZoneId.of("UTC"))
val timestamp = formatter.format(currentTime)
val actual = Utils.stringToMillis(timestamp) // Testing function defined above
actual should be ( expected )
Code: Select all
1739424250000 was not equal to 1739424250305
Es gibt einen geringfügigen Unterschied und daher fällt der Testfall fehl. Ist das erwartet? Gibt es irgendwelche Arbeiten?
Code: Select all
val difference = Math.abs(expected - actual)
difference should be < 1000L // less than 1 second