计算秒数直到Joda时间的给定日期和时间

时间:2013-02-20 20:00:52

标签: scala akka jodatime

我正在尝试使用Joda Time库来帮助我安排向Akka的演员发送一些消息。

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

我想考虑夏令时(确保它总是在8:30左右开火,而不是7:30或9:30),所以我将使用LocalDateLocalTime。< / p>

所以,基本上,我有:

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

rightNow = LocalDateTime.now()

我想知道基于targetDateTimetargetDate撰写targetTime的最佳方式是什么,所以我可以用它来计算与rightNow的时差

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

2 个答案:

答案 0 :(得分:0)

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

targetDateTime = targetDate.toLocalDateTime(targetTime)

secondsToWait = Seconds.secondsBetween(rightNow, targetDateTime)

答案 1 :(得分:0)

如果您拥有targetDatetargetTime(如问题中所示),则很容易获得targetDateTime:

targetDateTime = targetDate.toDateTime(targetTime);

从现在到Duration之间获取targetDateTime的秒数:

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

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

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

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

免责声明:我刚才看到这是一个scala问题,所以你可能需要纠正任何语法差异,因为我不熟悉scala。