0

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?

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

Regex is slow. For this u need not use regex , u can use string.substring .

val today="20150403"
val out=today.substring(0,4)+"-"+today.substring(4,6)+"-"+today.substring(6)

1 Comment

Or f"${today}%.4s-${today.drop(4)}%.2s-${today.drop(6)}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.