scala Datetime parse
Scala DateTime Parse
To parse a datetime in Scala, you can use the java.time.format.DateTimeFormatter
class. Here's an example of how to parse a datetime string using this class:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
val datetimeString = "2023-11-29T12:34:56"
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
val datetime = LocalDateTime.parse(datetimeString, formatter)
println(datetime)
In this example, we first define the datetime string that we want to parse ("2023-11-29T12:34:56"
). Then, we create a DateTimeFormatter
object with the desired format pattern ("yyyy-MM-dd'T'HH:mm:ss"
). Finally, we use the parse
method of the LocalDateTime
class to parse the datetime string into a LocalDateTime
object.
The output of the above code will be the parsed datetime object, which can be further processed or used as needed.
Please note that the format pattern used in the example ("yyyy-MM-dd'T'HH:mm:ss"
) assumes that the datetime string follows the ISO 8601 format. If your datetime string has a different format, you will need to adjust the format pattern accordingly.