John John - 1 year ago 127
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?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download