Rem*_*oon 2 java scala scala-java-interop
I have a scala val function as follows :
val getTimestampEpochMillis = (year:Int, month:Int, day:Int, hour:Int, quarterHour:Int, minute:Int, seconds:Int) => {
var tMinutes = minute
var tSeconds = seconds
if(minute == 0){
if(quarterHour == 1){
tMinutes = 22
tSeconds = 30
}else if(quarterHour == 2){
tMinutes = 37
tSeconds = 30
}else if(quarterHour == 3){
tMinutes = 52
tSeconds = 30
}else if(quarterHour == 0){
tMinutes = 7
tSeconds = 30
}
}
val localDateTime = LocalDateTime.of(year, month, day, hour, tMinutes, tSeconds)
localDateTime.atZone(ZoneId.of("GMT")).toInstant().toEpochMilli()
}
Run Code Online (Sandbox Code Playgroud)
当我从Java调用此函数时,出现以下错误:
[ERROR] required: no arguments
[ERROR] found: int,int,int,int,int,int,int
[ERROR] reason: actual and formal argument lists differ in length
Run Code Online (Sandbox Code Playgroud)
调用scala val函数的Java代码:
Long minTimestampOfGranularity = getTimestampEpochMillis(year, 1, 1, 0, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
尝试
getTimestampEpochMillis().apply(year, 1, 1, 0, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
并注意括号()在getTimestampEpochMillis()。使用javap我们检查生成的类
public scala.Function7<java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, scala.runtime.BoxedUnit> getTimestampEpochMillis();
Run Code Online (Sandbox Code Playgroud)
在这里看到getTimestampEpochMillis()return scala.Function7,因此我们必须先调用,getTimestampEpochMillis()然后才能apply使用参数。
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |