I am trying to convert a date format of that looks like this: 2011-09-30 00:00:00.0 to 20110930 in scala. Does anyone have any ideas?
If all you want to do is to change date format from string to string all you may do something similar to:
def toSimpleDate(dateString: String): Option[String] = {
val parser = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S")
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd")
Try {
LocalDateTime.parse(dateString, parser)
}.toOption
.map(_.format(formatter))
}
toSimpleDate("2011-09-30 00:00:00.0") // Some("20119030")
toSimpleDate("Meh") // None
Use something like this:
import java.text.SimpleDateFormat
val inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S")
val outputFormat = new SimpleDateFormat("ddMMyyyy")
val date = "2015-01-31 12:34:00.0"
val formattedDate = outputFormat.format(inputFormat.parse(date))
println(formattedDate) //20150131
You can probably use the Date Functions as mentioned in other answers. However if you are sure about the format to be 2011-09-30 00:00:00.0
A simple Map operation should be fine
val x = List("2011-09-30 00:00:00.0")
val output = x map (x => x.dropRight(11).replace("-",""))
> output: List[String] = List(20110930)
However this solution works If and Only If You can guarantee the input comes in the same format.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.