Conversion of Joda Date Time to SQL Timestamp and Vice Versa

Introduction

This tutorial shows how you 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

Java – any version, Maven 3.6.1 – 3.6.3, Gradle 5.6 – 6.7.1, Joda API 2.10.3 – 2.10.10

Project Setup

Create a gradle or maven based project in your favorite IDE or tool. The project’s name is joda-to-sql-date-vice-versa.

The build.gradle script is updated to include the Joda API as shown in the below:

plugins {
    id 'java-library'
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    jcenter()
}

dependencies {
    implementation 'joda-time:joda-time:2.10.3' to 2.10.10
}

If you are using Maven based project then you can use below dependency in your pom.xml file:

<!-- joda date time -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10.3 to 2.10.10</version>
</dependency>

Main Class

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

package com.roytuts.joda.to.sql.date.vice.versa;

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

import org.joda.time.LocalDateTime;

public class JodaToSqlAndViceVersa {

	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);
	}

}

Testing the Application

Now run the above main class and you will see the following output in the console.

LocalDateTime : 2021-02-24T12:03:56.684
LocalDateTime to SQL Date : 2021-02-24
LocalDateTime to SQL Timestamp : 2021-02-24 12:03:56.684
SQL Timestamp to LocalDateTime : 2021-02-24T12:03:56.684

Source Code

Download

Leave a Reply

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