Search code examples
calendarzoneddatetimejsr310threetenbp

converting ZonedDateTime to Calendar using ThreeTen-Backport (JSR 310)


I am trying to convert a ZonedDateTime object to a Calendar object using the back-port of the java.time classes (JSR 310): the ThreeTen-Backport project.

My understanding was that the back-port was supposed to be similar to functionality on Java 8 (without being on Java 8 itself). Currently, the system is on Java 7.

Whenever I try to execute the following code:

ZonedDateTime zdt = ....
GregorianCalendar newCal = GregorianCalendar.from(zdt);

I get the following message:

The method from(ZonedDateTime) is undefined for the type GregorianCalendar

I used the following dependency:

<dependency>
    <groupId>org.threeten</groupId>
    <artifactId>threetenbp</artifactId>
    <version>1.3.6</version>
</dependency>

How can one come about this problem?

Any help hints or advice would be greatly appreciated.

TIA


Solution

  • org.threeten.bp.DateTimeUtils

    The backport cannot add methods to the JDK. See DateTimeUtils for the conversion methods.

    ZonedDateTime zdt = ....
    GregorianCalendar newCal = DateTimeUtils.toGregorianCalendar(zdt);