I need to migrate also this code:
private int calculate(OffsetDateTime expirationDate) {
return Seconds.secondsBetween(DateTime.now(), expirationDate).getSeconds();
}
to this code:
private int calculate(OffsetDateTime expirationDate) {
return Duration.between(OffsetDateTime.now(), expirationDate).getSeconds();
}
getSeconds returns long. is there some better way to get int?
No; it is trivially imaginable that the gap, in seconds, between 2 OffsetDateTime objects is more than 2^31-1 seconds. (more than about 4 billion).
If you know it cannot possibly be such a large gap, just cast to int. The somewhat ugly 'int' cast is your visible sign in the code that somebody made an assumption that it won't be a problem. In other words, that cast is a GOOD thing.
If you're not quite sure and you want an exception instead, you have to program that. Fortunately, Java has a built-in function for this:
That will throw if the returned amount is so large, casting it to an
intwould overflow the value.