2011-01-11 94 views
44

我想在执行一些更多的计算之前简单地将TimeZone信息添加回LocalDate。 LocalDate来自于使用ObjectLab LocalDateCalculator将日期添加到现有DateTime,但该方法需要返回修改后的ReadableInstant以形成一个Interval,然后我可以检查它。如何把Joda Localdate转换为Joda DateTime?

我想达乔达的LOCALDATE到乔达日期时间转换的代码:

LocalDate contextLocalBusinessDate = calculator.getCurrentBusinessDate(); 
DateTime businessDateAsInContextLocation = new DateTime(contextLocalBusinessDate, contextTimeZone); 

我得到的错误是从Joda的转换系统:

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) 

我在寻找解决此问题的方法或解决方案,该方法会导致带有完整时区信息的精确时间间隔。

回答

66

上有LocalDate用于此目的的各种方法,包括:

你必须明确你想要的是什么实时组件将在重新暗示DateTime对象,这就是为什么DateTime的通用转换构造函数无法完成它。

+11

现货。有一点需要注意:toDateTimeAtStartOfDay并不意味着“午夜”...有些时区实施夏令时更改,以便从午夜到凌晨1点跳过。 – 2011-01-11 10:28:31