Scala Date Examples: Formatting, Converting and other Useful Operations on Dates

Last updated:

WIP ALERT This is a work in progress.

Converting date string from one format to another

Suppose you want to convert the date string "2015-01-31 12:34:00" (the input format) to another format, say "31/01/2015 12:34" (the output format).

import java.text.SimpleDateFormat

val inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val outputFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm")

val date = "2015-01-31 12:34:00"

val formattedDate = outputFormat.format(inputFormat.parse(date))

println(formattedDate) // prints "31/01/2015 12:34"

Dialogue & Discussion