将时间跨度(long和TimeUnit)添加到java.util.Date的最佳方法?

thr*_*ups 3 java time

我正在尝试实现此方法:

/**
 * Adds the given span to the given date and returns a new date.
 */
public java.util.Date add(java.util.Date d, long span, java.util.concurrent.TimeUnit unit) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我可以在设备上进行切换.有没有这样做的图书馆?阿帕奇公地?乔达?

Jon*_*eet 8

您无需打开设备 - 只需要询问正确的毫秒数:

return new Date(d.getTime() + unit.toMillis(span));
Run Code Online (Sandbox Code Playgroud)

另一方面,我肯定会尝试使用Joda代替:)