What is the difference of using TemporalAmount or TemporalUnit in Java 8?

前端 未结 2 960
悲哀的现实
悲哀的现实 2021-02-19 01:23

I write some piece of code in Java 8 which use time arithmetic. I realize that I can implement in differentways. Lets look at simple code below. Of course it is the same result

2条回答
  •  我寻月下人不归
    2021-02-19 01:52

    If you look at the source, the plus(long amountToAdd, TemporalUnit unit) method uses the plusXXX methods to produce the results. So there are no arguments here about efficiency.

    Instead, you use whichever is most appropriate for your scenario. I would suggest that if you are using user input to decide whether to add hours, minutes, etc., then the plus() method is better. Otherwise, your code might be easier to read if you use plusXXX.

提交回复
热议问题