Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly use Java libraries in Scala

Tags:

java

scala

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>
like image 284
Devyn Collier Johnson Avatar asked Dec 12 '22 08:12

Devyn Collier Johnson


1 Answers

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())
like image 109
Reimeus Avatar answered Dec 24 '22 20:12

Reimeus