I'm migrating some signatures from f(long dur, TimeUnit timeUnit)
to f(Duration duration)
, and would like to implement the former with the latter.
Being on Java 8, I can't find any API to easily convert the long
+TU
to a Duration
, the only idea that comes to me is to do something ugly like:
static Duration convert(long dur, TimeUnit timeUnit) {
switch (timeUnit) {
case DAYS:
return Duration.ofDays(dur);
case HOURS:
/* alternative, but (again) I don't have an easy conversion from TimeUnit -> ChronoUnit */
return Duration.of(dur, ChronoUnit.HOURS);
case ..... /* and so on */
}
}
Or did I miss some API?
To achieve this, the class stores a long representing seconds
, so actually we got into undefined behaviour with the above corner-case. – Dennadennard