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.

2

1 Answer 1

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:_*)
Sign up to request clarification or add additional context in comments.

1 Comment

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 <and> (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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.