2013-02-20 99 views
0

我想使用乔达时间库来帮助我安排发送一些消息给阿卡的演员。计算秒数,直到在约达时间的给定日期和时间

我想安排每天上午8:30发送电子邮件。为此,我必须告诉调度程序需要等待多少秒(或几毫秒),直到发送下一条消息。

我想解释夏令时(为了确保它总是在8:30左右开火,而不是7:30或9:30),所以我将使用LocalDateLocalTime

所以,基本上,我有:

targetDate = LocalDate.now().plusDays(1)targetTime = new LocalTime(8, 30)

rightNow = LocalDateTime.now()

我想知道什么是基于targetDatetargetTime所以targetDateTime组成的最佳方式我可以用它来计算时差rightNow

我知道我可以创建一个新的LocalDateTime从我的targetDatetargetTime中提取构造函数的所有值,但有没有更优雅的方法?

回答

0

到目前为止,我已经解决了:

targetDateTime = targetDate.toLocalDateTime(targetTime)

secondsToWait = Seconds.secondsBetween(rightNow, targetDateTime)

+2

您可能感兴趣的[scala-time](https://github.com/jorgeortiz85/scala-time) – EECOLOR 2013-02-20 22:09:04

0

获得targetDateTime很容易,如果你有targetDatetargetTime(在你的问题中给出):

targetDateTime = targetDate.toDateTime(targetTime); 

从现在开始到现在为止,获取Duration的秒数targetDateTime

new Duration(new DateTime(), targetDateTime).getStandardSeconds(); 

,因为它假定每秒的方法被称为标准秒为1000毫秒的第二标准。正如其javadoc所说,目前所有的Chronics都只有标准的秒数。

但你也可以简单地使用毫秒(无需转换假设):

new Duration(new DateTime(), targetDateTime).getMillis(); 

声明:我刚刚看到这是一个Scala的问题,所以你可能必须更正任何语法差异,因为我不熟悉scala。