How to convert Joda Localdate to Joda DateTime?

Simon Gibbs picture Simon Gibbs · Jan 11, 2011 · Viewed 55.7k times · Source

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.

Answer

skaffman picture skaffman · Jan 11, 2011

There are various methods on LocalDate for this, including:

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.