4

I am learning Scala and I am unable to figure out how to properly use commands from Java libraries in Scala. I show below what am trying to do in a command-line.

collier@Nacho-Laptop:~$ scala
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import java.text._
import java.text._                                               ^


scala> println(java.text.DateFormat.getCalendar())
<console>:11: error: value getCalendar is not a member of object java.text.DateFormat
              println(java.text.DateFormat.getCalendar())
                                           ^

scala> println(java.text.getCalendar())
<console>:11: error: object getCalendar is not a member of package java.text
              println(java.text.getCalendar())
                                ^

scala> println(getCalendar())
<console>:11: error: not found: value getCalendar
              println(getCalendar())
                      ^

scala> getCalendar()
<console>:11: error: not found: value getCalendar
              getCalendar()
              ^

scala>
2
  • 1
    getCalendar is not static on DateFormat. Commented Jan 9, 2014 at 22:24
  • 1
    And because of that your problem lies not on the Scala side, but on the Java side. The equivalent Java code would also fail. Commented Jan 10, 2014 at 8:45

2 Answers 2

8

The REPL error messages clearly show that getCalendar is not defined for the DateFormat class. You can simply call Calendar#getInstance()

scala> import java.util.Calendar
import java.util.Calendar

scala> println(Calendar.getInstance().getTime())
Sign up to request clarification or add additional context in comments.

Comments

5
java.text.DateFormat.getCalendar()

will not work in Java because getCalendar is an instance method of DateFormat.

If you want to get a calendar try using Calendar.getInstance(Locale)

3 Comments

Thanks, but how would I do this in Scala? What do I import and how do I type this?
import java.util.Calendar println(Calendar.getInstance)
@DanW thanks! Okay, I understand how to do this now.

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.