How to convert Joda Localdate to Joda DateTime?

戏子无情 提交于 2019-12-09 04:27:44

问题


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.

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);

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.


回答1:


There are various methods on LocalDate for this, including:

  • LocalDate::toDateTimeAtCurrentTime()
  • LocalDate::toDateTimeAtStartOfDay()
  • LocalDate::toDateTime( LocalTime )
  • LocalDate::toDateTime( LocalTime , DateTimeZone )

You have to be explicit about what you want the time component to be in the resulting DateTime object, which is why DateTime's general-conversion constructor can't do it.



来源:https://stackoverflow.com/questions/4656524/how-to-convert-joda-localdate-to-joda-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!