Conversion of Joda Date Time to SQL Timestamp and Vice Versa

This tutorial shows how we can convert joda date time to SQL timestamp or vice versa.

Java’s standard date and time classes (mainly java.util.Date and java.util.Calendar) have limited functionality and have a number of design problems. The fact that many of constructors and methods of java.util.Date are deprecated.

Joda Time has a better thought out API than what is available in Java’s date and time standard library. There are classes for timestamps with or without a timezone, classes for holding only a date (year, month, day) or only a time of day, classes for periods, durations and intervals, it supports the ISO 8601 format (which is the standard format in XML documents) and much more.

Now JDK 8 is out and it has a new date and time API which is similar to Joda Time. For more information please read package java.time.
Prerequisites

JDK 1.6 or later
Eclipse 3.2 or later
Maven 2 or later
Joda Date Time API

Now we will see how we can make this happen step by step

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

maven-arctype-quickstart

Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : joda-date-time

Step 2. Add joda-time dependency to the pom.xml file.

<!-- joda date time -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.7</version>
</dependency>

Step 3. Write a class which will show how to use joda date time and convert them to SQL timestamp or vice versa

import java.sql.Date;
import java.sql.Timestamp;

import org.joda.time.LocalDateTime;

public class JodaDateTimeTest {

  /**
   * @param args
   */
  public static void main(String[] args) {
    LocalDateTime localDateTime = new LocalDateTime();
    System.out.println("LocalDateTime : " + localDateTime);
    System.out.println("LocalDateTime to SQL Date : " + jodatToSQLDate(localDateTime));
    Timestamp timestamp = jodaToSQLTimestamp(localDateTime);
    System.out.println("LocalDateTime to SQL Timestamp : " + timestamp);
    System.out.println("SQL Timestamp to LocalDateTime : " + sqlTimestampToJodaLocalDateTime(timestamp));
  }

  public static Date jodatToSQLDate(LocalDateTime localDateTime) {
    return new Date(localDateTime.toDateTime().getMillis());
  }

  public static Timestamp jodaToSQLTimestamp(LocalDateTime localDateTime) {
    return new Timestamp(localDateTime.toDateTime().getMillis());
  }

  public static LocalDateTime sqlTimestampToJodaLocalDateTime(Timestamp timestamp) {
    return LocalDateTime.fromDateFields(timestamp);
  }

}

Step 4. Now run the above class and see the output in the console.

LocalDateTime : 2015-06-11T12:08:57.320
LocalDateTime to SQL Date : 2015-06-11
LocalDateTime to SQL Timestamp : 2015-06-11 12:08:57.32
SQL Timestamp to LocalDateTime : 2015-06-11T12:08:57.320

That’s all. Thank you for reading.

Soumitra Roy Sarkar

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *