Convert Joda-Time DateTime into Calendar and Vice Versa in Java

Tags: Joda Time Joda DateTime Calendar Locale Convert

In this Joda-Time tutorial, we learn how to convert Joda-Time’s DateTime object into java.util.Calendar object and vice versa from java.util.Calendar object into Joda-Time’s DateTime object in Java.

Add Joda Time library to your Java project

To use Joda Time Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile group: 'joda-time', name: 'joda-time', version: '2.10.9'

To use Joda Time Java library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10.9</version>
</dependency>

To download the Joda Time .jar file you can visit Joda Time releases page at github.com/JodaOrg/joda-time

Convert DateTime into Calendar using DateTime.toCalendar() method

DateTimeToCalendar.java

import org.joda.time.DateTime;

import java.util.Calendar;
import java.util.Locale;

public class DateTimeToCalendar {
    public static void main(String[] args) {
        DateTime dateTime = DateTime.now();

        Calendar calendar = dateTime.toCalendar(Locale.getDefault());

        System.out.println(calendar.toString());
    }
}
The output is:
java.util.GregorianCalendar[time=1610984963653,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Bangkok",offset=25200000,dstSavings=0,useDaylight=false,transitions=3,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2021,MONTH=0,WEEK_OF_YEAR=4,WEEK_OF_MONTH=4,DAY_OF_MONTH=18,DAY_OF_YEAR=18,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=10,HOUR_OF_DAY=22,MINUTE=49,SECOND=23,MILLISECOND=653,ZONE_OFFSET=25200000,DST_OFFSET=0]

Create New Calendar object from DateTime object using constructor

CalendarToDateTime.java

import org.joda.time.DateTime;

import java.util.Calendar;

public class CalendarToDateTime {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();

        DateTime dateTime = new DateTime(calendar);

        System.out.println(dateTime);
    }
}
The output is:
2021-01-18T22:49:42.323+07:00

Happy Coding 😊

Convert Joda-Time DateTime into Date in Java

Joda-Time Create UTC DateTime in Java

Joda Time DateTime by Examples in Java

Joda-Time Create DateTime with Time Start Of the Day in Java

Convert SQL Timestamp into Joda-Time DateTime and Vice Versa in Java

Convert Joda-Time DateTime into String and Vice Versa in Java

Format Joda-Time DateTime String using ISODateTimeFormat in Java

Compare Joda-Time DateTime using DateTimeComparator in Java

Joda-Time Create DateTime with Time End of the Day in Java

Get Date and Time fields value of Joda-Time DateTime object in Java