我正在学习Scala,我无法弄清楚如何正确使用Scala中Java库的命令。我在下面展示了在命令行中尝试做什么。
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>
答案 0 :(得分:8)
REPL错误消息清楚地表明getCalendar
类没有定义DateFormat
。您只需致电Calendar#getInstance()
scala> import java.util.Calendar
import java.util.Calendar
scala> println(Calendar.getInstance().getTime())
答案 1 :(得分:5)
java.text.DateFormat.getCalendar()
在Java中不起作用,因为getCalendar
是DateFormat
的实例方法。