1

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.

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
Anjum
  • 29
  • 1
  • 4

1 Answers1

5

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:_*)
jwvh
  • 50,871
  • 7
  • 38
  • 64
  • with solution 1, I am getting below error. Error:(54, 20) overloaded method value format with alternatives: (x$1: java.util.Locale,x$2: String,x$3: Object*)String (x$1: String,x$2: Object*)String cannot be applied to (String, Int) println(String.format("%04d-%02d-%02d",dd:_*)) But second solution is working for me. Thank you so much. – Anjum May 20 '20 at 09:48