Just use the method substring()on the line. Note that Scala strings behave like Java strings (with some additional methods), therefore everything that java.lang.Stringcan also be used in Scala strings.
val s = "20100903"
val t = s.substring(0, 4)
(Note that the arguments are not the length and the starting index, but the starting index (inclusive) and the ending index (exclusive)).
But when it comes to date parsing, why don't you just use java.text.SimpleDateFormatit like in Java?
val s = "20100903"
val fmt = new SimpleDateFormat("yyyyMMdd")
val date = fmt.parse(s)
source
share