Java 8:计算两个 ZonedDateTime 之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41077142/
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
Java 8: Calculate difference between two ZonedDateTime
提问by Maggie Hill
I'm trying to write a method to print the time difference between two ZonedDateTimes, regarding the difference between time zones.
我正在尝试编写一种方法来打印两个ZonedDateTime之间的时差,关于时区之间的差异。
I found some solutions but all of them were written to work with LocalDateTime.
我找到了一些解决方案,但所有这些解决方案都是为与LocalDateTime一起使用而编写的。
采纳答案by Micha? Szewczyk
You can use method betweenfrom ChronoUnit.
您可以使用方法之间的ChronoUnit。
This method converts those times to same zone (zone from the first argument) and after that, invokes untilmethod declared in Temporalinterface:
此方法将这些时间转换为相同的区域(来自第一个参数的区域),然后调用直到在Temporal接口中声明的方法:
static long zonedDateTimeDifference(ZonedDateTime d1, ZonedDateTime d2, ChronoUnit unit){
return unit.between(d1, d2);
}
Since both ZonedDateTimeand LocalDateTimeimplements Temporalinterface, you can write also universal method for those date-time types:
由于ZonedDateTime和LocalDateTime 都实现了Temporal接口,因此您还可以为这些日期时间类型编写通用方法:
static long dateTimeDifference(Temporal d1, Temporal d2, ChronoUnit unit){
return unit.between(d1, d2);
}
But keep in mind, that invoking this method for mixed LocalDateTimeand ZonedDateTimeleads to DateTimeException.
Hope it helps.
但请记住,为混合的LocalDateTime和ZonedDateTime调用此方法会导致DateTimeException。
希望能帮助到你。
回答by Basil Bourque
tl;dr
tl;博士
For hours, minutes, seconds:
对于小时、分钟、秒:
Duration.between( zdtA , zdtB ) // Represent a span-of-time in terms of days (24-hour chunks of time, not calendar days), hours, minutes, seconds. Internally, a count of whole seconds plus a fractional second (nanoseconds).
For years, months, days:
年、月、日:
Period.between( // Represent a span-of-time in terms of years-months-days.
zdtA.toLocalDate() , // Extract the date-only from the date-time-zone object.
zdtB.toLocalDate()
)
Details
细节
The Answer by Michal Sis correct, showing ChronoUnit
.
Michal S的答案是正确的,显示ChronoUnit
。
Duration
& Period
Duration
& Period
Another route is the Duration
and Period
classes. Use the first for shorter spans of time (hours, minutes, seconds), the second for longer (years, months, days).
另一种途径是Duration
和Period
类。第一个用于较短的时间跨度(小时、分钟、秒),第二个用于较长时间(年、月、日)。
Duration d = Duration.between( zdtA , zdtB );
Produce a String in standard ISO 8601 formatby calling toString
. The format is PnYnMnDTnHnMnS
where the P
marks the beginning and T
separates the two portions.
通过调用以标准 ISO 8601 格式生成字符串toString
。格式是PnYnMnDTnHnMnS
其中P
标记的开始和T
两个部分分开。
String output = d.toString();
In Java 9 and later, call the to…Part
methods to get the individual components. Discussed in another Answer of mine.
在 Java 9 及更高版本中,调用to…Part
方法来获取单个组件。在我的另一个答案中讨论过。
Example code
示例代码
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdtStart = ZonedDateTime.now( z );
ZonedDateTime zdtStop = zdtStart.plusHours( 3 ).plusMinutes( 7 );
Duration d = Duration.between( zdtStart , zdtStop );
2016-12-11T03:07:50.639-05:00[America/Montreal]/2016-12-11T06:14:50.639-05:00[America/Montreal]
PT3H7M
2016-12-11T03:07:50.639-05:00[美国/蒙特利尔]/2016-12-11T06:14:50.639-05:00[美国/蒙特利尔]
PT3H7M
Interval
& LocalDateRange
Interval
& LocalDateRange
The ThreeTen-Extraproject adds functionality to the java.time classes. One of its handy classes is Interval
to represent a span of time as a pair of points on the timeline. Another is LocalDateRange
, for a pair of LocalDate
objects. In contrast, the Period
& Duration
classes each represent a span of time as notattached to the timeline.
该ThreeTen-EXTRA项目将功能添加到java.time类。它的一个方便的类是Interval
将时间跨度表示为时间线上的一对点。另一个是LocalDateRange
,用于一对LocalDate
对象。相比之下,Period
&Duration
类每个都代表一个时间跨度,而不是附加到时间线。
The factory method for Interval
takes a pair of Instant
objects.
的工厂方法Interval
接受一对Instant
对象。
Interval interval = Interval.of( zdtStart.toInstant() , zdtStop.toInstant() );
You can obtain a Duration
from an Interval
.
您可以Duration
从Interval
.
Duration d = interval.toDuration();
About java.time
关于java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*
classes.
您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.timeclasses.
- For earlier Android (<26), the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9、Java SE 10及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- java.time类的更高版本的 Android 捆绑实现。
- 对于早期的 Android(<26),ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。