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?
a date object has a toString(format: String)
method.
date.toString(format)
2017-05-07
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With