Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: printing a DateTime object in specific format

Tags:

scala

I have a DateTime object and I want to print it with a given format, let's say yyyy-MM-dd.

I've tried

val date = DateTime.now()
val format = "yyyy-MM-dd"
println(date.formatted(format))

but got yyyy-MM-dd as if the format wasn't recognized.

I also tried

val formatter = new SimpleDateFormat(format)
println(

but got

Cannot format given Object as a Date java.lang.IllegalArgumentException: Cannot format given Object as a Date

How do I print the DateTime object in the format of my choosing?

like image 700
Dotan Avatar asked Jan 04 '23 01:01

Dotan


1 Answers

a date object has a toString(format: String) method.

date.toString(format)

2017-05-07

like image 137
Dotan Avatar answered Jan 11 '23 09:01

Dotan