Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

Joda-Time is a very popular Java library concerning date and time manipulation. It gives a much more intuitive and flexible API than that usually offered by the standard DateTime class.

In this tutorial, we’ll look at how to transform Joda-Time DateTime objects into standard Java Date ones and vice versa.

2. Setting Up Joda-Time

First, we should ensure that our project includes the joda-time library:

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

Alternatively, we can download the jar file and put it in our classpath.

3. Convert Joda-Time DateTime to Java Date

To convert a Joda-Time DateTime object to a standard Java Date we use the method called toDate(). Below is a simple example:

@Test
public void givenJodaDateTime_whenConvertingToJavaDate_thenConversionIsCorrect() {
    DateTime jodaDateTime = new DateTime();
    java.util.Date javaDate = jodaDateTime.toDate();
    assertEquals(jodaDateTime.getMillis(), javaDate.getTime());
}

In this test method, we create a new instance of DateTime from Joda-Time named jodaDateTime. Subsequently, we called the toDate() method on this Joda DateTime instance to obtain a corresponding java.util.Date object.

The test is performed using the assertEquals method, which asserts that the time in milliseconds retrieved from the original Joda DateTime object equals the time obtained from the new DateTime object created through the java.util.Date.

4. Convert Java Date to Joda-Time DateTime

It is also straightforward to convert a plain Java Date object into Joda-Time DateTime. We can use the DateTime constructor designed for java.util.Date parameter as follows:

@Test
public void givenJavaDate_whenConvertingToJodaDateTime_thenConversionIsCorrect() {
    java.util.Date javaDate = new java.util.Date();
    DateTime jodaDateTime = new DateTime(javaDate);
    assertEquals(javaDate.getTime(), jodaDateTime.getMillis());
}

Within the above test method, we actively instantiate a new java.util.Date object, representing the current date and time. Subsequently, we create a corresponding Joda DateTime object using the provided Java Date. The actual validation occurs using the assertEquals method, where we verify that the time in milliseconds is retrieved from the original java.util.Date object is equal to the time represented by the Joda DateTime object

5. Conclusion

In conclusion, one of the usual operations when working with dates and time in Java is converted between Joda-Time DateTime objects and standard Java Date.

Now that we have gone through the examples presented above, it should be easy for us to implement Joda-Time in our projects and easily convert these two types.

As usual, the accompanying source code can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.