怎么把Joda Localdate转换成Joda DateTime? [英] How to convert Joda Localdate to Joda DateTime?
问题描述
我试图在执行更多计算之前将TimeZone信息简单地添加回LocalDate中. LocalDate来自使用ObjectLab LocalDateCalculator向现有的DateTime添加天数,但是该方法需要返回修改后的ReadableInstant来形成一个Interval,然后我可以检查该Interval.
I'm trying to simply add TimeZone information back into a LocalDate before performing some more calculations. The LocalDate came from using the ObjectLab LocalDateCalculator to add days to an existing DateTime but the method needs to return a modified ReadableInstant to form an Interval which I can then inspect.
我正在尝试的代码相当于将Joda LocalDate转换为Joda DateTime:
The code I'm trying amounts to a conversion of Joda LocalDate to Joda DateTime:
LocalDate contextLocalBusinessDate = calculator.getCurrentBusinessDate();
DateTime businessDateAsInContextLocation = new DateTime(contextLocalBusinessDate, contextTimeZone);
我收到的错误来自Joda的转换系统:
The error I get is from Joda's conversion system:
java.lang.IllegalArgumentException: No instant converter found for type: org.joda.time.LocalDate
at org.joda.time.convert.ConverterManager.getInstantConverter(ConverterManager.java:165)
at org.joda.time.base.BaseDateTime.<init>(BaseDateTime.java:147)
at org.joda.time.DateTime.<init>(DateTime.java:192)
我正在寻找解决此问题的方法,或者是一种解决方法,它可以产生具有完整时区信息的准确间隔.
I'm looking for a fix to this problem, or a workaround that results in an accurate Interval with full timezone information.
推荐答案
There are various methods on LocalDate
for this, including:
-
LocalDate::toDateTimeAtCurrentTime()
-
LocalDate::toDateTimeAtStartOfDay()
-
LocalDate::toDateTime( LocalTime )
- 您必须明确要在生成的
DateTime
对象中添加时间分量,这就是DateTime
的常规转换构造函数无法做到的原因.You have to be explicit about what you want the time component to be in the resulting
DateTime
object, which is whyDateTime
's general-conversion constructor can't do it.这篇关于怎么把Joda Localdate转换成Joda DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!