Durations
Frequently, some length of time is represented as a long
in Java. The trouble here is that you don't always know what time unit the long
represents and as Effective Java recommends, you'll often end up with something like the following
1
|
|
or you might come across unhelpful code like this;
1
|
|
This is all very error prone, ugly and in the second case, fails to convey the intent. The Duration
class captures a length of time and forces you to express the time unit. So, for example,
1
|
|
Using Duration
in your own method forces your clients to create use an explicit time unit and you can convert back explicitly. for example,
1 2 3 4 5 |
|