我正在尝试使用Joda Time库来帮助我安排向Akka的演员发送一些消息。
我想安排每天上午8:30发送电子邮件。为此,我必须告诉调度程序在发送下一条消息之前要等待多少秒(或毫秒)。
我想考虑夏令时(确保它总是在8:30左右开火,而不是7:30或9:30),所以我将使用LocalDate
和LocalTime
。< / p>
所以,基本上,我有:
targetDate = LocalDate.now().plusDays(1)
和targetTime = new LocalTime(8, 30)
和
rightNow = LocalDateTime.now()
我想知道基于targetDateTime
和targetDate
撰写targetTime
的最佳方式是什么,所以我可以用它来计算与rightNow
的时差{ p>
我知道我可以创建一个新LocalDateTime
从我的targetDate
和targetTime
中提取构造函数的所有值,但是:有更优雅的方法吗?
答案 0 :(得分:0)
到目前为止,我已经解决了:
targetDateTime = targetDate.toLocalDateTime(targetTime)
secondsToWait = Seconds.secondsBetween(rightNow, targetDateTime)
答案 1 :(得分:0)
如果您拥有targetDate
和targetTime
(如问题中所示),则很容易获得targetDateTime:
targetDateTime = targetDate.toDateTime(targetTime);
从现在到Duration
之间获取targetDateTime
的秒数:
new Duration(new DateTime(), targetDateTime).getStandardSeconds();
该方法称为标准秒,因为它假定每秒都是1000毫秒的标准秒。正如其javadoc所说,目前所有的Chronologies都只有标准的秒数。
但你也可以简单地使用毫秒(不需要转换假设):
new Duration(new DateTime(), targetDateTime).getMillis();
免责声明:我刚才看到这是一个scala问题,所以你可能需要纠正任何语法差异,因为我不熟悉scala。