John John - 7 months ago 64
Scala Question

String format using regex in Scala

I have a String in the format of "20150403" and I want to convert it into the format of string like "2015-04-03". Is there any way using regular expression and string format in Scala?


Here is a sample

val date = """(\d{4})(\d{2})(\d{2})""".r
val date(year, month, day) = "20150402"
println(s"$year-$month-$day") // 2015-04-02