在 Java 8 中使用 TemporalAmount 或 TemporalUnit 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28963110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference of using TemporalAmount or TemporalUnit in Java 8?
提问by Ugur Artun
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 but I confused which way is mostly applied or most efficient to make arithmetic operations in Java 8 ?
我在 Java 8 中编写了一些使用时间算法的代码。我意识到我可以以不同的方式实施。让我们看看下面的简单代码。当然它是相同的结果,但我混淆了在 Java 8 中进行算术运算最常用或最有效的方法是什么?
LocalTime time = LocalTime.now();
// 1st way
LocalTime plusOp = time.plus(Duration.ofMinutes(10L));
// 2nd way
LocalTime plusOp2 = time.plus(10L, ChronoUnit.MINUTES);
System.out.println(plusOp);
System.out.println(plusOp2);
// 3. way simply
time.plusMinutes(10L);
Thanks in advance.
提前致谢。
回答by Jon Skeet
Duration
can only handle fixed-length periods, such as "hours", "minutes", "seconds", "days" (where it assumes exactly 24 hours per day). You can't use "months" with Duration
, because a month varies in length.
Duration
只能处理固定长度的时间段,例如“小时”、“分钟”、“秒”、“天”(假设每天正好是 24 小时)。您不能将“月”与 一起使用Duration
,因为一个月的长度各不相同。
Period
- the other common TemporalAmount
implementation - represents years, months and days separately.
Period
- 另一个常见的TemporalAmount
实现 - 分别代表年、月和日。
Personally I would recommend:
我个人建议:
- When you know the unit beforehand, use the appropriate
plusXxx
method, e.g.time.plusMinutes(10)
. That's about as easy to read as it gets. - When you're trying to represent "logical" calendar amounts, use
Period
- When you're trying to represent "fixed length" amounts, use
Duration
- 当您事先了解单位时,请使用适当的
plusXxx
方法,例如time.plusMinutes(10)
。这很容易阅读。 - 当您尝试表示“逻辑”日历金额时,请使用
Period
- 当您尝试表示“固定长度”数量时,请使用
Duration
Here's an example of where Period
and Duration
can differ:
以下是 wherePeriod
和Duration
可以不同的示例:
import java.time.*;
public class Test {
public static void main(String[] args) {
ZoneId zone = ZoneId.of("Europe/London");
// At 2015-03-29T01:00:00Z, Europe/London goes from UTC+0 to UTC+1
LocalDate transitionDate = LocalDate.of(2015, 3, 29);
ZonedDateTime start = ZonedDateTime.of(transitionDate, LocalTime.MIDNIGHT, zone);
ZonedDateTime endWithDuration = start.plus(Duration.ofDays(1));
ZonedDateTime endWithPeriod = start.plus(Period.ofDays(1));
System.out.println(endWithDuration); // 2015-03-30T01:00+01:00[Europe/London]
System.out.println(endWithPeriod); // 2015-03-30T00:00+01:00[Europe/London]
}
}
I wouldn't worry about the efficiency until you really need to - at which point you should have a benchmark so you can test different options.
在您真正需要之前,我不会担心效率 - 此时您应该有一个基准,以便您可以测试不同的选项。
回答by Duncan Jones
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.
如果您查看来源,则该plus(long amountToAdd, TemporalUnit unit)
方法使用plusXXX
方法来产生结果。所以这里没有关于效率的争论。
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
.
相反,您可以使用最适合您的场景的任何一种。我建议如果您使用用户输入来决定是否添加小时、分钟等,那么该plus()
方法更好。否则,如果您使用plusXXX
.