Reputation: 806
I am getting error while converting java to kotlin and cannot understand how to solve this particular error.
internal fun getDiff(to: Calendar, from: Calendar): Long {
var diffInSeconds = (to.time.time - from.time.time) / 1000
val diff = longArrayOf(0, 0, 0, 0)
diff[3] = if (diffInSeconds >= 60) diffInSeconds % 60
else diffInSeconds // sec
diff[2] = if ((diffInSeconds = diffInSeconds / 60)>= 60)
diffInSeconds % 60
else
diffInSeconds // min
diff[1] = if ((diffInSeconds = diffInSeconds / 60) >= 24)
diffInSeconds % 24
else
diffInSeconds // hour
diff[0] = (diffInSeconds = diffInSeconds / 24) // day
Log.e("days", diff[0].toString() + "")
return diff[0]
}
Following line: (diffInSeconds = diffInSeconds / 60)
shows error displaying
Assignments are not expressions, and only expressions are allowed in this context
Upvotes: 1
Views: 13355
Reputation: 3976
The syntax is not valid, because diffInSeconds = diffInSeconds / 60
is not an expression in Kotlin. just do this
var a = diffInSeconds /= 60
diff[1] = if (a >= 24)
Upvotes: 1
Reputation: 443
You can not do things like this:
diffInSeconds = diffInSeconds / 60
in if, it is not supported by kotlin. You have to extract it before or after if.
E.g.
internal fun getDiff(to: Calendar, from: Calendar): Long {
var diffInSeconds = (to.time.time - from.time.time) / 1000
val diff = longArrayOf(0, 0, 0, 0)
diff[3] = if (diffInSeconds >= 60) diffInSeconds % 60
else diffInSeconds // sec
diffInSeconds /= 60
diff[2] = if (diffInSeconds >= 60)
diffInSeconds % 60
else
diffInSeconds // min
diffInSeconds /= 60
diff[1] = if (diffInSeconds >= 24)
diffInSeconds % 24
else
diffInSeconds // hour
diffInSeconds /= 24
diff[0] = (diffInSeconds) // day
return diff[0]
}
Upvotes: 1