从 joda Duration 中减去/添加时间

subtracting / adding time from joda Duration

例如,我知道 joda 的 DateTime class 中的 .minusDays(int).plus(int) 方法。

但是当我使用 joda Duration 时,没有附加特定的时间点,有没有一种简单而好的方法来减去或增加特定的时间量,比如一天?

类似于:

new Duration(startTime, endTime).minusDays(2);

巧妙地使用 API 调用(例如使用 Duration#withDurationAdded),似乎 您可以从 Duration 中减去一天,只要它是 "standard" 天(一天 86,400 秒),并且只要您将标量运算设置为负数即可。

new Duration(start, end).withDurationAdded(Duration.standardDays(1), -1);

tl;博士

Joda-Time 提供方法 Duration::plus & Duration::minus.

myDuration.minus(                // A span of time unattached to the timeline. Consists of a number of milliseconds.
    Duration.standardDays( 2 )   // Generic 24-hour days.
)

最好迁移到 java.time:

myDuration.minus(                // A span of time unattached to the timeline. Consists of a number of whole seconds plus a number of nanoseconds.
    Duration.ofDays( 2 )         // Generic 24-hour days.
)

没有

Duration(startTime, endTime).minusDays(2)

Joda-Time 中的

A Duration 表示毫秒数。这个时间跨度没有附在时间轴上。所以没有“天”的概念。

引用the documentation:

An immutable duration specifying a length of time in milliseconds.

A duration is defined by a fixed number of milliseconds. There is no concept of fields, such as days or seconds, as these fields can vary in length.

因此,虽然您不能 add/subtract ,但您可以 add/subtract 毫秒数是通用一天的 24 小时周期,而不考虑日历问题,不考虑夏令时 (DST) 等异常情况。

使用 java.util.concurrent.TimeUnit 枚举计算该毫秒数。

long millis = TimeUnit.DAYS.toMilliseconds( 2 ) ; // 2 days of 24 hours * 60 minutes per hour * 60 seconds per minute * 1,000 milliseconds per second.

Duration twoGenericDays = Duration.millis( millis ) ;

Duration class 为这项工作提供了一个捷径,静态方法 Duration.standardDays 其中“标准”表示与日历无关的通用 24 小时时间块。

Duration twoGenericDays = Duration.standardDays( 2 ) ;  // Two generic 24-hour days, unrelated to the calendar.

添加。

Duration d = myDuration.plus( twoGenericDays ) ;

减去。

Duration d = myDuration.minus( twoGenericDays ) ;

java.time

Joda-Time 项目现在处于维护模式,建议迁移到其继任者 java.time classes.

与上面讨论的概念相同,其中 java.time.Duration represents a span of time unattached to the timeline. Except the resolution is finer: nanoseconds rather than milliseconds。从技术上讲,Duration 由 (a) 整数秒数和 (b) 构成小数秒的纳秒数组成。

Duration twoGenericDays = Duration.ofDays( 2 ) ;  // Two generic days, each being a 24 hours long.

添加。

Duration d = myDuration.plus( twoGenericDays ) ;

减去。

Duration d = myDuration.minus( twoGenericDays ) ;

像 Joda-Time 一样,java.timeclasses 是建立在 immutable objects 模式之上的。不是改变(“变异”)一个对象,而是实例化一个新的独立对象。


关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* classes.

从哪里获得java.time classes?

ThreeTen-Extra project 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.