Anjum
Anjum

Reputation: 39

How to use String.format() in scala

I am new to Scala world, I wanted to use String.format() to create a date format string. I have three integer value year month and day, I wanted to change it in yyyy-mm-dd. String.format() expect an array of Anyref, when I am creating Array[Anyref] by passing integer value to it, it is throwing below error.

Error:(49, 30) the result type of an implicit conversion must be more specific than AnyRef dd(2) = inputCalendar.get(5)

My full example is :

val dd = new Array[AnyRef](3);
dd(0) = Integer.valueOf(inputCalendar.get(1))
dd(1) = Integer.valueOf(inputCalendar.get(2) + 1)
dd(2) = inputCalendar.get(5)
println(String.format("%04d-%02d-%02d",dd))

Note: I don't want to use any Date API for this.

Upvotes: 1

Views: 1094

Answers (1)

jwvh
jwvh

Reputation: 51271

Declare dd elements as type Int and this should work.

val dd = new Array[Int](3)
. . . //unchanged
String.format("%04d-%02d-%02d",dd:_*)

Or ...

"%04d-%02d-%02d".format(dd:_*)

Upvotes: 5

Related Questions